-
April 3rd, 2001, 09:35 PM
#1
Detect EOF in readLine using LineNumberReader/BufferedReader
Dear all,
I wonder if it is possible to check in readLine operation that the line read is the last line.
In my program, I repeatedly Read data into my own object (construct new object, read data then put it into a vector). null pointer exception occurred when I tried to read past the end of file.
Is it possible to check (before readLine()) operation that EOF is reached?
-
April 4th, 2001, 05:59 AM
#2
Re: Detect EOF in readLine using LineNumberReader/BufferedReader
It isn't clear from your message whether you're using LineNumberReader.read() or LineNumberReader.readLine(). LineNumberReader.readLine() returns null when EOF is reached, so if you test the returned String before trying to use it, you shouldn't get a null pointer exception. Without seeing the relevant code, I can't really say more. This is the kind of thing:FileReader fr = new FileReader("MyFile.txt");
LineNumberReader lnr = new LineNumberReader(fr);
String line = lnr.readLine();
while (line != null) {
... // do something with line
line = lnr.readLine();
}
If you're using LineNumberReader.read() you should test the returned int for -1 (EOF).
Dave
To email me remove '_spamjam' from my email address
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
March 6th, 2003, 11:01 AM
#3
How would you detect an EOF with BufferedReader though?
I am reading a config file where I have empty lines, so i can't use the usual "while (line.readLine() != null)" bit.
Sample of config file:
#
# myapp.cfg
#
# Date: 3/25/03
#
# The name of the log file
LogFile = /logs/myapp.log
-
March 6th, 2003, 12:08 PM
#4
Ron - why not just try it and see?
BufferedReader returns null when EOF and an empty String ("") when a line with only a line terminator is found.
Works for me.
The empirical approach resolves all doubt...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
March 6th, 2003, 04:02 PM
#5
-
May 24th, 2011, 08:50 AM
#6
Re: Detect EOF in readLine using LineNumberReader/BufferedReader
this will detect EOF before you read the last line:
int readAheadLimit = 1024;
boolean endOfFile = false;
reader.mark(readAheadLimit);
String tmp = reader.readLine();
if(tmp==null) endOfFile = true;
reader.reset();
-
May 24th, 2011, 10:55 AM
#7
Re: Detect EOF in readLine using LineNumberReader/BufferedReader
Originally Posted by jeffzfcheng
this will detect EOF before you read the last line...
You revived an 8 year old thread to give an unnecessarily convoluted solution to a problem that was solved more simply 8 years ago?
Why?
Experience is a poor teacher: it gives its tests before it teaches its lessons...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|