I'm using BufferedReader and readLine to read a textfile into a program.
The lines are terminated by CRLF strings.
The problem is that there are some empty lines, that is, there are
sequences of CRLFCRLF without any interverning characters. The readLine
throws a NoSuchElementException, and never reaches the end of the file.
How can I get readLine to read the whole file and terminate at the end?
Here is some test code:
try {
int i = 0;
File f = new File(args[i]);
System.out.println("Begin Processing " + f.toString() + " ..." +
f.toURL() + " ...");
br = new BufferedReader(new FileReader(f));
System.out.println("Now Processing " + " ...");
try {
while ((line=br.readLine())!=null) { // lines are terminated by CRLF
System.out.println("Line " + " ..." + line);
processSCALine(line);
} // while
}
catch (Exception e) {
System.out.println("Exception reading: " + e.toString() + " Line " +
" ..." + line);
}
finally {
br.close();
}
}
catch (Exception e) {
System.out.println("Exception opening: " + e.toString());
return;
}
}

Signature
In memorium Layal Najib
www.cpj.org
Eric Sosman - 15 Aug 2006 17:22 GMT
Tom Peel wrote On 08/15/06 12:01,:
> I'm using BufferedReader and readLine to read a textfile into a program.
> The lines are terminated by CRLF strings.
> The problem is that there are some empty lines, that is, there are
> sequences of CRLFCRLF without any interverning characters. The readLine
> throws a NoSuchElementException, and never reaches the end of the file.
> [...]
Since your code catches the exception and doesn't print
the stack trace, how do you know the exception is thrown by
readLine() and not by processSCALine()?

Signature
Eric.Sosman@sun.com
Tom Peel - 16 Aug 2006 10:21 GMT
Eric Sosman schrieb:
> Tom Peel wrote On 08/15/06 12:01,:
>> I'm using BufferedReader and readLine to read a textfile into a program.
[quoted text clipped - 7 lines]
> the stack trace, how do you know the exception is thrown by
> readLine() and not by processSCALine()?
Eric (and Daniel)
You are both quite correct, the exception is being thrown in
processSCALine. Thanks for the help pointing out my error.
T.
dsjoblom@abo.fi - 15 Aug 2006 17:22 GMT
> I'm using BufferedReader and readLine to read a textfile into a program.
> The lines are terminated by CRLF strings.
> The problem is that there are some empty lines, that is, there are
> sequences of CRLFCRLF without any interverning characters. The readLine
> throws a NoSuchElementException, and never reaches the end of the file.
Huh? No it doesn't, or if it really does, you have a broken Java
implementation (not likely). Post the whole stacktrace for the
exception. The exception almost certainly occurs in your
processSCALine(line); method.
Regards,
Daniel Sjöblom