0

例如我有一个文件“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);
    }
4

3 回答 3

1

空行并不是真正的空白。终止每一行都涉及行尾字符。明显的空行意味着您有一对行尾字符相邻。

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

例如,假设我们有一个包含单词thisand的文件that

this

that

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

this<LF>
<LF>
that<LF>

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

this<LF><LF>that<LF>

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

于 2020-12-06T08:35:58.017 回答
1

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

        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 
于 2020-12-06T08:59:09.253 回答
0

我在代码中的 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 ]
于 2020-12-06T13:15:42.977 回答