1

ランダムな「DearJohnLetter」を生成できるようにすることを目標に、Grammar ファイルを読み込む (データ構造に分割する) 必要があるプロジェクトに取り組んでいます。

私の問題は、.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
4

1 に答える 1

1

私があなたを正しく理解していれば、次の行が空かどうかを行内で判断したいだけですか?

true の場合、キックオフの例を次に示します。

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
于 2010-03-09T01:05:57.210 に答える