java - EOFexception caused by empty file -
i created new file roomchecker
empty. when read it, throws me eofexception undesirable. instead want see that, if file empty run other 2 functions in if(roomfeed.size() == 0)
condition. write statement in eofexception catch clause; that's not want because every time when file read , reaches end of file execute functions. instead when file has data should specified in else.
file filechecker = new file("roomchecker.ser"); if(!filechecker.exists()) { try { filechecker.createnewfile(); } catch (ioexception e) { e.printstacktrace(); system.out.println("unable create new file"); } } try(fileinputstream fis = new fileinputstream("roomchecker.ser"); objectinputstream ois = new objectinputstream(fis)) { roomfeed = (list<roomchecker>) ois.readobject(); system.out.println("end of read"); if(roomfeed.size() == 0) { system.out.println("your in null if statement"); defaultroomlist(); uploadavailablerooms(); } else { for(int i=0; i<roomnumber.size(); i++) { for(int j=0; j<roomfeed.size(); i++) { if((roomnumber.get(i)).equals(roomfeed.get(i).getroomnumsearch())){ system.out.println("reach dead end now"); } else { defaultroomlist(); uploadavailablerooms(); } } } } } catch (ioexception ioe) { ioe.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); }
all this:
if(!filechecker.exists()) { try { filechecker.createnewfile(); } catch (ioexception e) { e.printstacktrace(); system.out.println("unable create new file"); } }
is complete waste of time, , 1 of 2 possible causes empty file problem. creating file can open , different problem instead of coping correctly original problem of file not being there isn't rational strategy. instead, should this:
if (filechecker.isfile() && filechecker.length() == 0) { // file 0 length: bail out }
and, in following code, this:
try(fileinputstream fis = new fileinputstream(filechecker); objectinputstream ois = new objectinputstream(fis)) { // ... } catch (filenotfoundexception exc) { // no such file ... } // other catch blocks before.
of course can still eofexception
if read file end, or if file incomplete, , still need handle that.
Comments
Post a Comment