• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EOFException keeps occuring after db read in

 
Sean Gildea
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting an EOFException when parsing the db file. Does anyone know how to stop the exception from occuring? The file is always successfully parsed to my liking, except an error occurs at the end. Here it is pasted in.




here is some of my code where i suspect the problem is, maybe someone can tell me what I am doing wrong, thanks


[ October 27, 2004: Message edited by: Sean Gildea ]
[ October 27, 2004: Message edited by: Sean Gildea ]
 
Matt Sheehan.
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of the for loop try:

This way you should never read past the end of the file and the EOFException won't be thrown.

Matt
 
Dieskun Koper
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put a try-catch (EOFExc) around the line that reads the flag at the start of each record. When the exeption occurs here, I know it's the end of the file.

Matt's solution looks better as no exception occurs at all, but in my method I only have an input stream, no File reference, so I did it differently.

Dies
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean,
In your for loops there is no guarantee that the file will be read even after the EOf is reached. To exit from both for loops give a "break" after
bytesRead += raf.read(bytes); //in the inner for loop
so that you exit both loops with single break. One way to to do this is by using label.

ex:


Pleae consider redesigning this part. The array length is hardcoded. It should be coming from the file metadata which may be provided. The outer for loop is not needed. All it does is checking for EOF. Many developers don't like using break. Please check code conventions to see whether any reference to break is there.

[Andrew: broke up increadibly long lines of code]
[ October 27, 2004: Message edited by: Andrew Monkhouse ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic