aspose file tools*
The moose likes I/O and Streams and the fly likes How to check EOF in Java?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to check EOF in Java??" Watch "How to check EOF in Java??" New topic
Author

How to check EOF in Java??

Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
Hello ,
How to check End of file in Java while reading a file?
regards,
Holla.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
This depends on the InputStream or Reader object you are using and the method you are using from that object. If you are using the readLine method of BufferedReader, it returns null on the next read after the last data is read off the stream. If you are using the read(byte[], int, int) of FileInputStream, it returns -1 when there is no more data to read.
Consult the API's for the Stream you are using to determine what it uses.
Hope this helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
Hi Carl,
What if File has 'null' character in between? That's the problem. I tried all the methods, and they are failing if file contains 'null' in middle. I tried to use skip, but it is not working. i don't understand why there is no endOfFile method in any of the class.
Any help in this matter?
Regards,
Holla
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Then you are using the wrong InputStream. Check the API's and determine which one will best suit your needs. If your still having problems, email me a copy of your code and the file in question if it's possible and I'll se what it needs
Darryl McCool
Greenhorn

Joined: Jan 03, 2001
Posts: 8
When a reader (and stream too I think) reaches the end of a file, it will throw a EOFException. Setup your try/catch block like so:
try {
// Code that reads file is here, inside a loop.
} catch (EOFException eofe) {
// This is okay. File has been completely read.
// Continue the execution oy the program.
} catch (IOException ioe) {
System.out.println(ioe);
}
Thus, when the EOFException is thrown, the end of the file has been reached and the reader will quit reading the file.
Hope this helps, if not, tell me how you want to read in the data (line at a time or byte at a time) and I can elaborate.

Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
What class are you trying to use? The upper level streams and readers don't recognize EOF because this is a concept for files only. They can be constructed on any stream, File, the screen, the keyboard, a pipe between applcations, a network socket etc. Give an example of your code an I might be able to help some more.
Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
Hi Carl,
I am trying to read using Reader objects. That's why I think I am not able to read EOF. Only Streams Methods throw EOFexception when end of file occurs.

But can you tell me why there is no method which tells End Of File in Java Classes, I mean in Readers?
regards,
Holla.
 
 
subject: How to check EOF in Java??