Search code examples
javafileobjectinputstream

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


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:2‌​577) 
  at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315) 
  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) 
  at profegraba.Persistencia.main(Persistencia.java:81)

Solution

  • 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();
        }
    }