wood burning stoves 2.0*
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
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: 960
    
  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: 960
    
  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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using DataInputStream (Handling EOFException)
 
Similar Threads
DataInputStream
New to Java, Simple I/O question
File and FileDescriptor
It doesnot print the content the file
readLine() Deprecated