java - 将输入和输出数据加载和保存到文本文件中(不起作用)

标签 java user-interface file-upload

我正在尝试从标题为“inData.txt”的文本文件加载程序的数据并将其输出到文件“out.txt”中,基本上模仿保存。问题是当我通过 GUI 运行此代码时,它不会加载到文本区域,但保存它可以工作。我不知道这是我的 GUI 代码的问题还是我的 League 文件本身的代码问题。

我尝试检查文本文件是否更改,但没有更改,但代码运行时没有错误。

//League.java

private static final String READ_FILE="inData.txt";
private static final String OUT_FILE="out.txt.";

public void readFromFile() //throwsIO exception /take from file into arraylist
{
    //reading the inData.txt file and loading the arraylist
    //with each instantiated object from the data
    try
    {
        Scanner in = new Scanner(new File(READ_FILE));
    //loop through the file and read each valie and create an object
    while(in.hasNextLine())
    {

        String name = in.nextLine();
        System.out.println ("read name: " + name);

        String coach = in.nextLine();
        System.out.println ("read coach: " + coach);

        int teamID = in.nextInt();
        System.out.println ("read team ID: " + teamID);

        int wins = in.nextInt();
        System.out.println ("read wins: " + wins);

        int losses = in.nextInt();
        in.nextLine();
        System.out.println ("read losses: " + losses);
        String player1 = in.nextLine();
        System.out.println ("read player 1 name: " + player1);
        String position1 = in.nextLine();
        System.out.println ("read player 1 position: " + position1);
        Player p1 = new Player(player1, position1);
        String player2 = in.nextLine();
        System.out.println ("read player 2 name: " + player2);
        String position2 = in.nextLine();
        System.out.println ("read player 2 position: " + position2);
        Player p2 = new Player(player2, position2);
        String player3 = in.nextLine();
        System.out.println ("read player 3: " + player3);
        String position3 = in.nextLine();
        System.out.println ("read player 3 position: " + position3);
        Player p3 = new Player(player3, position3);

        //create and Item object
        Team newTeam = new Team(name, coach, teamID, wins, losses, p1, p2, p3);
        league.add(newTeam);
    }
        }
    catch(IOException e)
    {
        System.out.println ("Error with file loading");
    }
}

readFromFile() 的预期结果是从 inData.txt 获取信息并将其导入 GUI。

最佳答案

您的文件内容是什么样的?

鉴于您是按行循环,我建议您始终使用 nextLine() 并根据需要解析从中获得的字符串。例如,如果您的文件内容如下所示:

line1
23:34:45:99
34

你可以这样做:

if (in.hasNextLine()){
   String firstLine = in.nextLine();
   String secondLine = in.nextLine();
   int[] nums = new int[3];
   int i = 0;
   for (String intString : lineTwo.split(":")) {
      nums[i] = Integer.valueOf(intString);
      i++;
   }
   // or simply:
   int thirdLine = Integer.valueOf(in.next());
   // if you just have one int per line anyway
}

我猜您遇到的问题涉及稍微更高级的主题,例如代码点或换行符和回车符之间的差异,如果您想了解 Scanner 的复杂性,您可能有兴趣了解这些主题。

请告诉我这是否确实不是您遇到的问题,我会编辑我的回复。

关于java - 将输入和输出数据加载和保存到文本文件中(不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56229910/

相关文章:

java - 长Java类路径解决方案

java - 如何在 [] 集定义中使用 $ 字符作为行尾字符?

java - 无需任何登录的 Waffle 授权

java - 在 Android 应用程序上显示非静态字符串?

ios - 在 swift 中,为什么我必须使用 IBaction 或 IBOutlet 在代码和 UI 之间进行通信?

php - laravel上传的图片未添加到数据库

java - 如何在java中导出一张图像的2个不同大小的图像

python - PySimpleGui 输出标题栏名称问题

validation - 在 PrimeFaces 的文件上传监听器中验证上传图像的尺寸

javascript - CKEditor 4.6 图片上传上传后不插入图片URL