Java:如何判断文本文件中的一行是否应该为空白?

发布于 2024-08-25 03:06:30 字数 773 浏览 6 评论 0原文

我正在开发一个项目,在该项目中我必须读取语法文件(将其分解为我的数据结构),目标是能够生成随机的“DearJohnLetter”。

我的问题是,在读取 .txt 文件时,我不知道如何确定该文件是否应该是完全空白的行,这对程序不利。

这是文件部分的示例,我如何判断下一行是否应该是空行? (顺便说一句,我只是使用缓冲阅读器)谢谢!


<start>
I have to break up with you because <reason> . But let's still <disclaimer> .

<reason>
<dubious-excuse>
<dubious-excuse> , and also because <reason>

<dubious-excuse>
my <person> doesn't like you
I'm in love with <another>
I haven't told you this before but <harsh>
I didn't have the heart to tell you this when we were going out, but <harsh>
you never <romantic-with-me> with me any more
you don't <romantic> any more
my <someone> said you were bad news

I'm working on a project in which I have to read in a Grammar file (breaking it up into my data structure), with the goal of being able to generate a random "DearJohnLetter".

My problem is that when reading in the .txt file, I don't know how find out whether the file was supposed to be a completely blank line or not, which is detrimental to the program.

Here is an example of part of the file, How do i tell if the next line was supposed to be a blank line? (btw I'm just using a buffered reader) Thanks!


<start>
I have to break up with you because <reason> . But let's still <disclaimer> .

<reason>
<dubious-excuse>
<dubious-excuse> , and also because <reason>

<dubious-excuse>
my <person> doesn't like you
I'm in love with <another>
I haven't told you this before but <harsh>
I didn't have the heart to tell you this when we were going out, but <harsh>
you never <romantic-with-me> with me any more
you don't <romantic> any more
my <someone> said you were bad news

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2024-09-01 03:06:30

如果我理解正确的话,你只是想确定一行内下一行是否为空?

如果为真,那么这是一个启动示例:

package com.stackoverflow.q2405942;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String... args) throws IOException {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream("/test.txt")));
            for (String next, line = reader.readLine(); line != null; line = next) {
                next = reader.readLine();
                boolean nextIsBlank = next != null && next.isEmpty();
                System.out.println(line + " -- next line is blank: " + nextIsBlank);
            }
        } finally {
            if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
        }
    }

}

这将打印以下内容:

<start> -- next line is blank: false
I have to break up with you because <reason> . But let's still <disclaimer> . -- next line is blank: true
 -- next line is blank: false
<reason> -- next line is blank: false
<dubious-excuse> -- next line is blank: false
<dubious-excuse> , and also because <reason> -- next line is blank: true
 -- next line is blank: false
<dubious-excuse> -- next line is blank: false
my <person> doesn't like you -- next line is blank: false
I'm in love with <another> -- next line is blank: false
I haven't told you this before but <harsh> -- next line is blank: false
I didn't have the heart to tell you this when we were going out, but <harsh> -- next line is blank: false
you never <romantic-with-me> with me any more -- next line is blank: false
you don't <romantic> any more -- next line is blank: false
my <someone> said you were bad news -- next line is blank: false

If I understand you right, you just want to determine inside a line whether the next line is empty?

If true, then here's a kickoff example:

package com.stackoverflow.q2405942;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String... args) throws IOException {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream("/test.txt")));
            for (String next, line = reader.readLine(); line != null; line = next) {
                next = reader.readLine();
                boolean nextIsBlank = next != null && next.isEmpty();
                System.out.println(line + " -- next line is blank: " + nextIsBlank);
            }
        } finally {
            if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
        }
    }

}

This prints the following:

<start> -- next line is blank: false
I have to break up with you because <reason> . But let's still <disclaimer> . -- next line is blank: true
 -- next line is blank: false
<reason> -- next line is blank: false
<dubious-excuse> -- next line is blank: false
<dubious-excuse> , and also because <reason> -- next line is blank: true
 -- next line is blank: false
<dubious-excuse> -- next line is blank: false
my <person> doesn't like you -- next line is blank: false
I'm in love with <another> -- next line is blank: false
I haven't told you this before but <harsh> -- next line is blank: false
I didn't have the heart to tell you this when we were going out, but <harsh> -- next line is blank: false
you never <romantic-with-me> with me any more -- next line is blank: false
you don't <romantic> any more -- next line is blank: false
my <someone> said you were bad news -- next line is blank: false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文