import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;
public class FileDemo implements Serializable
{
static ArrayList<FileDemo> list=new ArrayList<FileDemo>();
public static void main(String[] args) throws Exception
{
File file=new File("log.txt");
if(!file.createNewFile() && file.length()!=0)
{
FileInputStream fis=new FileInputStream("log.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
while(true)
{
try{
list.add((FileDemo)ois.readObject());
}
catch(EOFException e) {
break;
}
catch(Exception e) {
e.printStackTrace();
break;
}
}
ois.close();
fis.close();
}
Scanner scn=new Scanner(System.in);
int x=0;
while(x!=3)
{
System.out.println("MENU");
System.out.println("Enter 1 to add new object to file");
System.out.println("Enter 2 to display list size");
System.out.println("Enter 3 to exit");
System.out.print("Enter your choice: ");
x=scn.nextInt();
if(x==1)
{
FileDemo filedemo=new FileDemo();
list.add(filedemo);
FileOutputStream fos=new FileOutputStream("log.txt",true);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(filedemo);
oos.close();
fos.close();
System.out.println("Object Created");
}
else if(x==2)
{
System.out.println(list.size());
}
else if(x==3)
break;
else
System.out.println("Invaild choice");
}
}
}
Hi, In this program, everytime the user enters option 1 in the menu, a new object of the class is created, saved to the list and also saved to the file. When I try to read the objects from the file at the next execution of the program, it reads only the first object from the file and on the next readObject() call it raises an exception as follows:
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at FileDemo.main(FileDemo.java:25)
I would like to know how I can read all the objects stored in the file and add them to the list.
Thank You, Clement