在java中,如何从用空行分隔的文件中读取数据

egdjgwm8  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(514)

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

ev7lccsx1#

空行并不是真正的空行。每行的终止都涉及到行尾字符。一个明显的空行意味着你有一对行尾字符邻接。
搜索这一对,找到后中断输入。例如,使用 String::split .
例如,假设我们有一个包含单词的文件 this 以及 that .

this

that

让我们可视化这个文件,显示换行符(lf)字符(unicode代码点10十进制),用于将每行终止为 <LF> .

this<LF>
<LF>
that<LF>

对于计算机而言,没有“行”,因此文本在java中显示为:

this<LF><LF>that<LF>

现在您可以更清楚地注意到换行符(lf)对是如何分隔每一行的。搜索该配对的示例以解析文本。

uoifb46i

uoifb46i2#

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

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
06odsfpq

06odsfpq3#

我在代码的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 ]

相关问题