#Deserialization file

9 messages · Page 1 of 1 (latest)

verbal iceBOT
#

Hey, @pale jacinth!
Please remember to /close this post once your question has been answered!

#
I have a problem. In one class I serialize my List of Objects to file. Every object in list have own number like: 1,2,3 etc.

   public static void save() throws FileNotFoundException {
    FileOutputStream fos = new FileOutputStream("src\\tc.ser");
    try {
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(players);
        oos.close();
        fos.close();
    }
    catch (FileNotFoundException e) {
        throw new IllegalArgumentException("File not found");
    } catch (IOException e) {
        throw new IllegalArgumentException("Error initializing");
    }
In different class i deserialize file and want to add new Object.

 FileInputStream fi = new FileInputStream("src\\tc.ser");
    try {
        ObjectInputStream oi = new LookAheadObjectInputStream(fi);
        players= (List<Players>) oi.readObject();
        oi.close();
        fi.close();
    } catch (IOException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
I have a problem after print list:

Player 1
Player 2
Player 3
When I add new Player i have:

Player 1
Player 2
Player 3
Player 1
Player 2
Why? How to do this to have a access to old class and add new Player 4 etc?

Different method serialize
brittle pewter
#

Dude we can't read minds. What is "after print list"?

#

What is "when I add new Players"?

#

You're touching semi-advanced subjects here, you shouldn't need to be told the obvious about describing your situation

pale jacinth
brittle pewter
#

What you just said. How is it done, what is the code and all. Or did you think that code was irrelevant when programming?

pale jacinth