【问题标题】:How read data from file that is separated by a blank line in Java如何从Java中以空行分隔的文件中读取数据
【发布时间】:2020-12-06 08:29:33
【问题描述】:

例如我有一个文件“input.txt”:

This is the
first data

This is the second
data

This is the last data
on the last line

我想以这种形式将这些数据存储在 ArrayList 中:

[This is the first data, This is the second data, This is the last data on the last line]

注意:文件中的每个数据都以空行分隔。如何跳过这个空行? 我尝试了这段代码,但它不能正常工作:

    List<String> list = new ArrayList<>();

    File file = new File("input.txt");

    StringBuilder stringBuilder = new StringBuilder();

    try (Scanner in = new Scanner(file)) {
        while (in.hasNext()) {
            String line = in.nextLine();
            if (!line.trim().isEmpty())
                stringBuilder.append(line).append(" ");
            else {
                list.add(stringBuilder.toString());
                stringBuilder = new StringBuilder();
            }
        }
    } catch (FileNotFoundException e) {
        System.out.println("Not found file: " + file);
    }

【问题讨论】:

    标签: java file java.util.scanner bufferedreader read-data


    【解决方案1】:

    空白行并不是真正的空白。有end-of-line 字符参与终止每一行。明显的空行意味着您有一对行尾字符相邻。

    搜索该对,并在找到时破坏您的输入。例如,使用 String::split 之类的东西。

    例如,假设我们有一个包含 thisthat 字样的文件。

    this
    
    that
    

    让我们可视化这个文件,将用于终止每一行的 LINE FEED (LF) 字符(Unicode 代码点十进制 10)显示为 &lt;LF&gt;

    this<LF>
    <LF>
    that<LF>
    

    对于计算机来说,没有“行”,所以文本在 Java 中看起来像这样:

    this<LF><LF>that<LF>
    

    您现在可以更清楚地注意到 LINE FEED (LF) 字符对是如何分隔每行的。搜索该配对的实例以解析您的文本。

    【讨论】:

      【解决方案2】:

      实际上你就快到了。您错过的是最后两行需要以不同方式处理,因为文件底部没有空字符串行。

              try (Scanner in = new Scanner(file)) {
                  while (in.hasNext()) {
                      String line = in.nextLine();
                      //System.out.println(line);
                      if (!line.trim().isEmpty())
                          stringBuilder.append(line).append(" ");
                      else { //this is where new line happens -> store the combined string to arrayList
                          list.add(stringBuilder.toString());
                          stringBuilder = new StringBuilder();
                      }
                  }
                  
                  //Below is to handle the last line, as after the last line there is NO empty line
                  if (stringBuilder.length() != 0) {
                      list.add(stringBuilder.toString());
                  } //end if
                  
                  for (int i=0; i< list.size(); i++) {
                      System.out.println(list.get(i));
                  } //end for
                  
              } catch (FileNotFoundException e) {
                  System.out.println("Not found file: " + file);
              }
      

      上面的输出:

      This is the first data 
      This is the second data 
      This is the last data on the last line 
      

      【讨论】:

        【解决方案3】:

        我在代码中的 while 循环之后添加了一个 if 代码,它起作用了,

            List<String> list = new ArrayList<>();
        
            File file = new File("input.txt");
        
            StringBuilder stringBuilder = new StringBuilder();
        
            try (Scanner in = new Scanner(file)) {
                while (in.hasNext()) {
                    String line = in.nextLine();
                    if (!line.trim().isEmpty()) {
                        stringBuilder.append(line).append(" ");
                    }
                    else {
                        list.add(stringBuilder.toString());
                        stringBuilder = new StringBuilder();
                    }
                }
                if (stringBuilder.toString().length() != 0) {
                    list.add(stringBuilder.toString());
                }
            } catch (FileNotFoundException e) {
                System.out.println("Not found file: " + file);
            }
        
            System.out.println(list.toString());
        

        我得到了以下输出

        [This is the first data , This is the second data , This is the last data on the last line ]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-15
          • 1970-01-01
          • 2012-06-13
          • 2013-08-04
          相关资源
          最近更新 更多