EOFException - how to handle?
While reading from the file, your are not terminating your loop. So its read all the values and correctly throws EOFException on the next iteration of the read at line below:
price = in.readDouble();
If you read the documentation, it says:
Throws:
EOFException - if this input stream reaches the end before reading eight bytes.
IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
Put a proper termination condition in your while loop to resolve the issue e.g. below:
while(in.available() > 0) <--- if there are still bytes to read
How to handle EOFException?
You can probably try doing this:
public AbstractBlock readBlock(int blockNum, AbstractDBFile f)
throws IOException {
DBFile dbf = (DBFile) f;
byte[] data2 = new byte[4096];
RandomAccessFile file = new RandomAccessFile(dbf.fileName, "r");
file.seek(4096+blockNum*4096);
Block b = new Block();
for (int i = 0; i < 4096; i++){
try{
data2[i] = file.readByte();
}catch(EOFException ex){
System.out.println("End of file reached!");
//break the loop
break;
}
}
file.close();
b.setData(data2);
return b;
}
Why am I getting java.io.EOFException?
Because you've reached the end of the file.
You seem to think that readObject() returns null at end of file. It doesn't. It returns null if and only if you wrote a null.
The correct test is to catch EOFException and break, not to read until readObject() returns null.
handling EOFException in java
The easiest way is to include the size of the list as the first thing written to the file. When you read the file, the first thing you retrieve is the size. Then you can read the expected number of objects.
Can't resolve java.io.EOFException?
while(o!=null)
This isn't a valid way to read an ObjectInputStream.
The readObject()
method only returns a null if you wrote a null. At end of stream it throws, guess what, an EOFException,
so the correct way to read the stream is to loop calling `readObject() until you catch that exception, then break and close the stream.
at the end of file it is giving EOF exception
That's exactly what EOFException
means.
EOFException Runtime Error while dealing with SealedObject
EOFException
just means you got to the end of the input. You need to close the inout and break out of the loop. You don't need to print the stack trace. It doesn't have anything to do with SealedObject
specifically, just with how object streams work.
You need to decide whether you're catching this exception or throwing it. You shouldn't do both.
Related Topics
How to Simulate a Real Mouse Click Using Java
Java Threads and Number of Cores
File Path Windows Format to Java Format
Use a .Jar Java Library API in C#
Is There Any Good and Free Date and Time Picker Available for Java Swing
How Does Java Makes Use of Multiple Cores
How to Modify JSONnode in Java
Java Static Serialization Rules
How to Set a Long Java Classpath in Windows
Why Is System.Arraycopy Native in Java
Is There a Java Equivalent to C#'s 'Yield' Keyword
How to Read Text from Hidden Element with Selenium Webdriver
What Is the Optimal Capacity and Load Factor for a Fixed-Size Hashmap