Java Fileinputstream Objectinputstream Reaches End of File Eof

Java FileInputStream ObjectInputStream reaches end of file EOF

readObject() doesn't return null at EOF. You could catch the EOFException and interpret it as EOF, but this would fail to detect distinguish a normal EOF from a file that has been truncated.

A better approach would be to use some meta-data. That is, rather than asking the ObjectInput how many objects are in the stream, you should store the count somewhere. For example, you could create a meta-data class that records the count and other meta-data and store an instance as the first object in each file. Or you could create a special EOF marker class and store an instance as the last object in each file.

Reading an object via ObjectInputStream throws EOF exception

  • You're trying to read objects from an empty file. Look at the stack trace. End of file trying to read the header. There isn't even a header in the file, let alone an object. It's empty.

  • Don't create a FileOutputStream until you have something to write to it, and don't forget to close it immediately afterwards.

  • You're also incorrectly converting the map to a String before writing it to the file, but you haven't yet got to the point where that's a problem.

JAVA objectinputstream cant drop out from the loop

This is a duplicate of this question :

Java FileInputStream ObjectInputStream reaches end of file EOF

Bottom line is that ObjectInputStream does not return null when it reaches the end of the stream. Instead, the underlying FileInputStream throws an EOFException. Although you could interpret that as the end of file, it does not allow you to distinguish a truncated file. So, practically speaking, ObjectInputStream expects you to know how many objects you will be reading in.

To get around this you could write an integer to the start of the file indicating how many UserRegistration objects are in the file. Read that value, then use a for loop to read that many objects.

Alternatively, you may be able to serialize your UserRegistration objects as an array or other container and then de-serialize the whole array/container.

Exception with ObjectInputStream while reading, and reaching the end of the file

That exception means that you reached the end of the file, but you still ask for more objects to be read. The program can't read any more, because it has reached the end of the file, so the exception is thrown.

As mentioned before, you should handle your resources more carefully.

You should catch the exception and handle it naturally.

ObjectInputStream objIn = null;
try {
FileInputStream fileIn = new FileInputStream("Serializado.txt");
if (fileIn == null) {
throw new IOException("Can't find file.");
}
objIn= new ObjectInputStream(fileIn);

while (true) {
obInp= (NewAlumno) objIn.readObject();
System.out.print("Nombre :" + obInp);
System.out.print(", Sexo: " + obInp.getSexo());
System.out.print(", Direccion: "+ obInp.getDireccion());
System.out.print(", Promedio: " + obInp.getpromedioPoo());
System.out.print(", Telefono: " + obInp.getTelefono()+"\n");
}
} catch (EOFException e) {
// Ignore or do whatever you wanted to signal the end of file.
} catch (Exception ex) {
ex.printStrackTrace();
} finally {
try {
if (objIn != null) {
objIn.close();
}
} catch (IOException closeException) {
closeException.printStackTrace();
}
}

End of File Exception on ObjectInputStream.readObject

Found out what was necessary to solve this. Thanks to @VGR's comment, I thought to pause the executing thread for 0.2 seconds if the file has been created less than a second ago.

if(System.currentTimeMillis()-files[i].lastModified()<1000){
Thread.sleep(200);

This prevents the exception and the application works now fine.

EOFexception in Java when reading objectinputstream

See the Javadoc. readObject() doesn't return null at EOF. It throws EOFException. The only way it can return a null is if you wrote a null at the other end, and that's not necessarily a good reason for terminating the read loop.

In short your code is wrong.

NB the initialization of 'o' is redundant.

NB (2) The code you posted cannot throw NullPointerException, unless masterFile is null. Is that a serious report or just a guess?



Related Topics



Leave a reply



Submit