Java check if file is empty: IOException: null -


i'm trying load file arraylist, program creates file if doesn't exist. i'm getting ioexception: null @ beginning file empty. how can avoid error , check if file empty or not? here's code:

     file f = new file(filename);       try {         if( !f.exists() ){             f.createnewfile();         }          inputstream = new objectinputstream(new fileinputstream(f));         scores = (arraylist<score>) inputstream.readobject();      } catch (ioexception e) {         system.out.println("io error: " + e.getmessage());      } {          ...      } 

use file.length() obtain size number of bytes of file:

the length, in bytes, of file denoted abstract pathname, or 0l if file not exist. operating systems may return 0l pathnames denoting system-dependent entities such devices or pipes.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -