while( inStream.hasNextLine() )
{
...
lineList.add( inStream.nextLine() );
}
...
lineList は ArrayList です。コードは、最後の行を取得しないことを除いて、すべてをうまく読み取っています。テキスト ファイルの最後の 2 行は、次のように終了します。
"a sentence here..."
<a blank line here. the blank line is the last line>
hasNextLine() がこの行の後に別の行を検出していないため、それを取得しないと思いますか?
最後の行を取得する方法は何ですか? EOFになるまで読んでから例外をキャッチすることはうまくいくかもしれないと思っていましたが、それを行う方法はないようです。
編集: 詳細情報
public void readLines()
{
lineList = new ArrayList();
try
{
inStream = new Scanner( new File( fileName ) );
}
catch( FileNotFoundException e )
{
System.out.println( "Error opening the file. Try again." );
}
if ( inStream != null )
{
while( inStream.hasNextLine() )
{
++originalLines;
lineList.add( inStream.nextLine() );
}
inStream.close();
}
}
全体の方法があります。何か間違っていますか?
編集:さらに詳しい情報
public static void main(String[] args)
{
Scanner inStream = null;
String test = "";
inStream = new Scanner( test );
while( inStream.hasNextLine() )
{
System.out.println( inStream.nextLine() );
}
}
空の文字列は取得されませんが、空白 " " は取得されます。