使用eclipe逐行读取txt文件

时间:2017-03-30 14:45:02

标签: java bufferedreader

代码:

    try {
    FileReader reader = new FileReader("rn.txt");
    BufferedReader bufferedReader = new BufferedReader(reader);

    String line;

    while ((line = bufferedReader.readLine()) != null) {
     System.out.println(line);
    }
    reader.close();

   } catch (IOException e) {
    e.printStackTrace();
   }

rn.txt文件内容:

12
45
78
23
45

控制台输出:

0
/
N
K
M
^

.
:
P
/
G
0

?


8
W
3

问题: 如何读取.txt文件中的实际数据?我写的代码有问题吗?

编辑:

this my console output and where i put my txt file

2 个答案:

答案 0 :(得分:2)

您的代码工作正常,检查您的.txt文件是否在正确的位置,并且您没有重复的.txt文件。

使用您的代码输出:

run:
12
45
78
23
45
BUILD SUCCESSFUL (total time: 0 seconds)

答案 1 :(得分:0)

  try {
                FileReader reader = new FileReader("F://angular pdf/rn.txt");
                BufferedReader bufferedReader = new BufferedReader(reader);

                String line =bufferedReader.readLine();

                while (line != null) {
                    System.out.println(line);
                   line= bufferedReader.readLine();
                }
                reader.close();

            } catch (IOException e) {
                e.printStackTrace();
            }