Reputation: 149
I'm doing a schedule book, and I'm saving a .txt all my objects, that works fine, the problem comes when I read them, my program reads , when the end of the file comes, my program doesn't know what to do, I won't put my code because it's a mess right now, but here's my teacher's code, which is the one that I'm using as a template to do mine:
try{
//***Instructions for reading***
//It creates a streamobject with the file's name and asigned format
FileInputStream fileIn = new FileInputStream("Serializado.txt");
//It creates an inputobject, so it can represent the object
ObjectInputStream objIn= new ObjectInputStream(fileIn);
while (fileIn != null) {
//with readObject() method, it extracts the object's content
obInp= (NewAlumno) objIn.readObject(); //Se hace un "cast" a NewAlumno
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");
}
objIn.close();
} catch(Exception e){}
}
As you can see, the catch exception it's empty, so, when i use my teacher's code, it looks like it's working flawlessly, but, i put a println there, and it always prints it. Meaning that something's wrong, and i'm pretty sure that is the
while(fileIn != null)
bacause Netbeans says that this expression is never null. So i'm guessing that the program doesn't know what to do after he reaches the end of the file... Any sugestions pals? Thanks in an advance!
Here is the exception:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2577)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at profegraba.Persistencia.main(Persistencia.java:81)
Upvotes: 1
Views: 4647
Reputation: 69329
The while loop is completely incorrect
while (fileIn != null) {
If your input file exists, then fileIn
will always be non-null. You should check if fileIn
is null immediately after constructing it, however, in case the file path is wrong.
Without the while loop, you are then just reading one object from the stream. Whether this is correct or not, I don't know. However, one should generally know how many objects were written to the stream so that the corresponding number of objects can be read back.
(You could read until you get an EOFException
, however I wouldn't personally condone that. I'd suggest you instead stored a container object with a number of sub-objects. An EOFException
may be thrown for reasons other than because the last object has been read successfully. However, I appreciate you may be forced to code this way to complete your assignment).
Finally, you need to ensure your input streams are closed, even if an exception occurs. If you are using Java 7, the try-with-resources construct can make this easier.
Upvotes: 2
Reputation: 5208
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();
}
}
Upvotes: 1
Reputation: 310859
ObjectInputStream.readObject()
throws EOFException
when the end of the stream is reached. Your loop should be of the form while (true)
or for (;;)
and it should contain a catch (EOFException exc)
block which breaks out of the loop.
Testing while (fileIn != null)
is entirely futile.
Upvotes: 2