File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Using DataInputStream (Handling EOFException) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using DataInputStream (Handling EOFException)" Watch "Using DataInputStream (Handling EOFException)" New topic
Author

Using DataInputStream (Handling EOFException)

Raman Ghai
Ranch Hand

Joined: Jan 01, 2012
Posts: 51
Here is a code that writes data to file and reads using DataInputStream and DataOutputStream


Exception


I am not able to understand how to get rid of this exception ?
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1037
    
  10

You get the exception because you try to read more data than you have written. You have two obvious choices -
1) Catch the exception and recognize that when you get an EOF exception that you have read all the data.
2) Write the number of data items to be written before you actually write them. You can then read read this number back to know how many data items to read.

I consider the first approach to be a hack and would recommend the second approach.


Raman Ghai
Ranch Hand

Joined: Jan 01, 2012
Posts: 51
Richard Tookey wrote:You get the exception because you try to read more data than you have written. You have two obvious choices -
1) Catch the exception and recognize that when you get an EOF exception that you have read all the data.
2) Write the number of data items to be written before you actually write them. You can then read read this number back to know how many data items to read.

I consider the first approach to be a hack and would recommend the second approach.




Yeah , actually the while loop created the exception . I removed it and using a counter to read it prices.length times and it worked..Thanks for replying.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1037
    
  10

Parminder Ghai wrote:
Yeah , actually the while loop created the exception . I removed it and using a counter to read it prices.length times and it worked..Thanks for replying.


Surely that can't be the correct approach since you are unlikely to have access to the 'prices' array when you read back the data. You need to write this length to the output file so it can be read back and used to create the correct length of array !
Raman Ghai
Ranch Hand

Joined: Jan 01, 2012
Posts: 51
Richard Tookey wrote:
Surely that can't be the correct approach since you are unlikely to have access to the 'prices' array when you read back the data. You need to write this length to the output file so it can be read back and used to create the correct length of array !


So you want to say that everytime someone writes some data to the file ,the length of the data is also written/updated to the file (different file or same file ?) ? what can be the other approach to know the length of data in such a file before reading the file ?
 
Consider Paul's rocket mass heater.
 
subject: Using DataInputStream (Handling EOFException)
 
Similar Threads
readLine() Deprecated
DataInputStream
It doesnot print the content the file
File and FileDescriptor
New to Java, Simple I/O question