Method#1
Reference URL: How can I read all objects in a file with ObjectInputStream? (I/O and Streams forum at Coderanch)
FileInputStream fis = new FileInputStream("input-file.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
while(fis.available() > 0) {
Object obj = ois.readObject()
...
}
Method#2
Reference URL: JavaMadeSoEasy.com (JMSE): Avoid ObjectInputStream.readObject() from throwing EOFException at End Of File in java
...
//write instance of EofIndicatorClass at EOF
oout.writeObject(new EofIndicatorClass());
...
/*
*If oin.readObject() returns instanceof EofIndicatorClass that means
*it's EOF, exit while loop and EOFException will not be thrown.
*/
Object obj;
while(!((obj = oin.readObject()) instanceof EofIndicatorClass)){
System.out.println(obj);
}
...
No comments:
Post a Comment