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


i'm doing schedule book, , i'm saving .txt objects, works fine, problem comes when read them, program reads , when end of file comes, program doesn't know do, won't put code because it's mess right now, here's teacher's code, 1 i'm using template mine:

try{   //***instructions reading***    //it creates streamobject file's name , asigned format    fileinputstream filein = new fileinputstream("serializado.txt");    //it creates inputobject, can represent object    objectinputstream objin= new objectinputstream(filein);    while (filein != null) {      //with readobject() method,  extracts object's content                                     obinp= (newalumno) objin.readobject(); //se hace un "cast" 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 can see, catch exception it's empty, so, when use teacher's code, looks it's working flawlessly, but, put println there, , prints it. meaning something's wrong, , i'm pretty sure

while(filein != null) 

bacause netbeans says expression never null. i'm guessing program doesn't know after reaches end of file... sugestions pals? in advance!

here exception:

java.io.eofexception    @ java.io.objectinputstream$blockdatainputstream.peekbyte(objectinputstream.java:2‌​577)    @ java.io.objectinputstream.readobject0(objectinputstream.java:1315)    @ java.io.objectinputstream.readobject(objectinputstream.java:369)    @ profegraba.persistencia.main(persistencia.java:81) 

that exception means reached end of file, still ask more objects read. program can't read more, because has reached end of file, exception thrown.

as mentioned before, should handle resources more carefully.

you should catch exception , handle 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 whatever wanted signal end of file. } catch (exception ex) {     ex.printstracktrace(); } {     try {         if (objin != null) {             objin.close();         }     } catch (ioexception closeexception) {         closeexception.printstacktrace();     } } 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -