Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using DataInputStream (Handling EOFException)

 
Raman Ghai
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic