objectinputstream

Java FileInputStream ObjectInputStream reaches end of file EOF

瘦欲@ 提交于 2019-11-26 20:54:38
I am trying to read the number of line in a binary file using readObject, but I get IOException EOF. Am I doing this the right way? FileInputStream istream = new FileInputStream(fileName); ObjectInputStream ois = new ObjectInputStream(istream); /** calculate number of items **/ int line_count = 0; while( (String)ois.readObject() != null){ line_count++; } readObject() doesn't return null at EOF. You could catch the EOFException and interpret it as EOF, but this would fail to detect distinguish a normal EOF from a file that has been truncated. A better approach would be to use some meta-data.

Appending to an ObjectOutputStream

假如想象 提交于 2019-11-25 23:02:48
问题 Is it not possible to append to an ObjectOutputStream ? I am trying to append to a list of objects. Following snippet is a function that is called whenever a job is finished. FileOutputStream fos = new FileOutputStream (preferences.getAppDataLocation() + \"history\" , true); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject( new Stuff(stuff) ); out.close(); But when I try to read it I only get the first in the file. Then I get java.io.StreamCorruptedException . To read I