java - Exception in reading a binary file -


i getting exception. because not catching them?

 java.io.eofexception @ java.io.objectinputstream$blockdatainputstream.peekbyte(objectinputstream.java:2577) @ java.io.objectinputstream.readobject0(objectinputstream.java:1315) @ java.io.objectinputstream.readobject(objectinputstream.java:369) @ readstudentrecord.readrecord(readstudentrecord.java:34) @ readstudentrecordtest.main(readstudentrecordtest.java:15) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:601) @ com.intellij.rt.execution.application.appmain.main(appmain.java:120) 

this class reads binary file:

 import java.io.*;   public class readstudentrecord {  private objectinputstream input;  public void openfile() {      try {         input = new objectinputstream(new fileinputstream("student.dat"));      } catch (ioexception e) {         e.printstacktrace();  //to change body of catch statement use file | settings | file templates.     } } // end of open file method  public void readrecord() {      q4 student;      system.out.println("student name\t\t student average mark");     try {          while (true) {               student = (q4) input.readobject();              system.out.println(student.getstudentname() + "\t\t\t" + student.averagemark(student.getmark()));                   }     } catch (classnotfoundexception e) {         e.printstacktrace();  //to change body of catch statement use file | settings | file templates.     } catch (ioexception e) {         e.printstacktrace();  //to change body of catch statement use file | settings | file templates.     }  }// end of readrecord method  public void closefile() {     try // close file , exit     {         if (input != null)             input.close();         system.exit(0);     } // end try     catch (ioexception ioexception) {         system.err.println("error closing file.");         system.exit(1);     } // end catch } // end method closefile } // end of class 

it works fine, except above exception, have no idea might causing it. can't figure out doing wrong.

you reading objects file in infinite loop.

while (true) {     student = (q4) input.readobject();         ... } 

eventually stream reaches end of file, still ask more objects, , that's why exception thrown.


a possible solution, catch eofexception, meaning no more objects exist.

try {     while (true) {         student = (q4) input.readobject();             ...     } } catch (eofexception eof) {     // reached end of file. here, if want.     // or else, ignore end of file, , proceed out of block. } {     // other stuff.     // not forget close stream.     input.close(); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -