【问题标题】:Java, FileReader() only seems to be reading the first line of text document?Java,FileReader() 似乎只读取文本文档的第一行?
【发布时间】:2017-04-14 18:08:44
【问题描述】:

我还在学习中,如果我有误解,请纠正我,但是 FileReader 对象不应该返回文本文件的全部内容吗?

我在这里有一个 sn-p 代码,我很简单地尝试获取一个短 .txt 文件的内容,并使用 system.out.println() 打印它

public class Main {

  public static void main(String[] args) throws FileNotFoundException, IOException {

    File testDoc = new File("C:\\Users\\Te\\Documents\\TestDocument.txt");
    BufferedReader reader = new BufferedReader(new FileReader(testDoc));
    Scanner in = new Scanner(new FileReader(testDoc));


    try {
      System.out.println(reader.readLine());
    } finally {
      reader.close();
    }
  }
}

.txt 文件只包含 3 行,格式如下:

some text here, more text and stuff
new estonian lessons
word = new word

但是程序只打印文件中的第一行。

some text here, more text and stuff

这是什么原因造成的,我该如何纠正?

我已尝试阅读文档,并通过 Stackoverflow 进行搜索,但我无法找到解决此问题的方法。

【问题讨论】:

  • 你不会错过某种循环吗? (请注意,有更多现代方式来读取文本文件)
  • readLine() 就是这样做的,只读取 1 行 - 这并不意味着 readFile()。你说你读过documentation,但我觉得这很难相信,因为文档的第一行声明“读取一行文本”,然后继续定义什么是一行文本。
  • 那是因为您只调用了一次readLine。为什么您会期望读取整个文件?
  • @shawn FileReader 不会读取文件的全部内容。实际考虑一下,如果文件很大并且 FileReader 读取整个文件,那么我们将开始遇到内存问题。
  • @RC。你会推荐什么?我的知识有限,我只是在一个关于异常和错误处理的演示中使用 readLine()

标签: java bufferedreader filereader


【解决方案1】:

BufferedReader的(看here)readLine()从文件中读取一行,所以需要写一个循环如下图:

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

另外,你的代码中不需要Scanner对象(如果你使用BufferedReader),所以它会简单如下所示:

        File testDoc = new File("C:\\Users\\Te\\Documents\\TestDocument.txt");
        BufferedReader reader = new BufferedReader(new FileReader(testDoc));
        try {
             String line = "";
             while((line =reader.readLine()) != null) {
                 System.out.println(line);
             }
        } finally {
          reader.close();
        }

【讨论】:

  • 那么 readLine() 是否会随着循环的每次重复自动移动到下一行? (还有另一个原因扫描仪在那里,我只是忘了删除它。)
  • 是的,你是对的,它将作为while的一部分移动
  • 似乎已经成功了,这里有很多有用的信息。谢谢!
【解决方案2】:

您可以使用实际实例化但不用于将其与FileReader 实例链接的扫描器。
它可以允许拥有具有hasNextLine()nextLine()Scanner 类的灵活api方法。

Scanner in = new Scanner(new FileReader(testDoc));


public static void main(String[] args) throws FileNotFoundException, IOException {

    File testDoc = new File("C:\\TestDocument.txt");
    Scanner in = new Scanner(new FileReader(testDoc));

    try {
        while (in.hasNextLine()) {
            String currentLine = in.nextLine();
            System.out.println(currentLine);
        }
    } finally {
        in.close();
    }
}

【讨论】:

    【解决方案3】:

    readLine() 方法只返回一行。所以你必须遍历所有行:

    while(reader.hasNextLine()){
        String currentLine = reader.readLine();
    }
    

    【讨论】:

    • BufferedReader 没有hasNextLine() 方法。 Scanner 有。
    • @davidxxx 我正要对此发表评论,但是是的,我在尝试调用 .hasNextLine() 时遇到了参考错误
    【解决方案4】:

    有两种方法。见下文。

     File testDoc = new File("C:\\Users\\Te\\Documents\\TestDocument.txt");
        BufferedReader reader = new BufferedReader(new FileReader(testDoc));
        Scanner in = new Scanner(new FileReader(testDoc));
        try {
    
          //Using Scanner Object
          while(in.hasNextLine()){
              System.out.println(in.nextLine());
          }
    
        //Using BufferReader Object
          String line=reader.readLine();
          while(line!=null){
              System.out.println(line);
              line=reader.readLine();
          }
    
        } finally {
          reader.close();
        }
    

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多