首页 > 解决方案 > 当我打印数组时,它只显示 txt 文件的最后一行而不是整个内容?

问题描述

这是我从 txt 文件中读取的代码,然后将其内容添加到索引为 10 的数组中,我希望它列出所有内容,但它只列出最后一个 10 次。

String filePath10 = "src//SalmonSortingEfficiencies//10nums.txt";
String filePath10000 = "src//SalmonSortingEfficiencies//10000nums.txt";
String start;
boolean eof;
if (rBtn10.isSelected()) {
    try {
        String temp;
        String phrase;
        FileReader fr = new FileReader(filePath10);
        //opens the stream to the text file
        BufferedReader br = new BufferedReader(fr);
        //Reads the data in the text file
        eof = false;
        while (!eof) {
            start = br.readLine();
            if (start == null) {
                eof = true;
                // sets eof to true if the name is null/seeing
                // where the end of the file is so br stops reading
            } else {
                temp = br.readLine();
                int smallArrayNumbers = Integer.parseInt(temp);
                phrase = smallArrayNumbers + "\n";
                for (int i = 0; i < 10; i++) {
                    smallArray[i] = phrase;
                }
            }
        }
        br.close();
    } catch (IOException e) {
        System.out.println("Error" + e);
        //looks and prints out any errors found
    }
    for (String item : smallArray) {
        txtONumbers.append(item);
    }
}

标签: javaarraysnetbeanstext-files

解决方案


推荐阅读