开发者

Java: how to tell if a line in a text file was supposed to be blank?

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 &l开发者_如何学Got;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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜