• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to check EOF in Java??

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,
How to check End of file in Java while reading a file?
regards,
Holla.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Raghavendra Holla
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic