I got a problem with Serialization and Deserialization in java. In my program whenever I create a file then I also creating a fileInfo object and then I serializing in a secure_store.dat file and after deserializing from that file also.For example I can create test1.txt with a new fileInfo object and serialize that fileInfo object then i can create test2.txt with again different fileInfo object and serialize it also without a problem. Whenever i deserialize the secure_store.dat I can easily see that 2 objects but the problem is whenever i close the program and reopen the program and create test3.txt with a fileInfo object and try to serialize, it deletes the 2 old object in the secure_store.dat file and whenever I deserialize the file I can only see the last object the others always deleted(which are created before the program closes). How i can solve this problem and return the all three objects ? Below you can see my code...
public static void serialization(ArrayList<FileInfo> allFiles) {
try {
FileOutputStream fileOut = new FileOutputStream("secure_store.dat");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(allFiles);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static ArrayList<FileInfo> deSerialization() throws FileNotFoundException {
ArrayList<FileInfo> arraylist = new ArrayList<FileInfo>();
try {
FileInputStream fileIn = new FileInputStream("secure_store.dat");
ObjectInputStream in = new ObjectInputStream(fileIn);
arraylist = (ArrayList<FileInfo>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return arraylist;
} catch (ClassNotFoundException c) {
System.out.println("Class not found");
c.printStackTrace();
return arraylist;
}
return arraylist;
}