0

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;
    }
erkevarol
  • 57
  • 2
  • 11
  • So when you're adding the new `FileInfo` into the `ArrayList` you are reading back the file first, then adding, then writing correct? Because if you aren't that is what you need to do. If the file is already saved, and you re-run your program its going to overwrite the existing info with the new info. So therefore why you only have the one. – 3kings Nov 13 '16 at 02:49
  • @3kings yeah I am doing that but i want to see all 3 objects whenever I re-run the program. Basically I want that at first that 2 object stays at the file and if i re-run the program and add new object to program, it should append that object to the file, it shouldn't overwrite it. – erkevarol Nov 13 '16 at 02:56
  • Maybe we aren't looking at the same code? Because i am clearly seeing nothing dealing with the fact that the "serialization" method should first read the existing `secure_store.dat` and then combine it with the new one you are passing in then write it. – 3kings Nov 13 '16 at 03:00

0 Answers0