1

Im trying to write sample data to a .txt file via an ObjectOutputStream. Afterwards i want to read the written result and show it in my application. Works perfect. But when i try to read the result after reopen the application it shows me only null values. Has someone an idea how to write this data permanently so that when i reopen my application the same data as before is shown after executing ObjectInputStream?

ObjectOutputStream method for adding a Team:

    public void addTeam(String teamName){

        try {

            Verein team = new Verein(teamName);


            ObjectOutputStream objectOutputStream =
                    new ObjectOutputStream(new FileOutputStream(c.pathDir+c.getPathTeams()));


            t.add(new Verein(teamName));
            objectOutputStream.writeObject(t);

            objectOutputStream.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

ObjectInputStream method for reading Teams fronm txt File:

public ArrayList<Verein> getTeams(){
    String teams="";
    ArrayList<Verein> vList = new ArrayList<Verein>();

    try {
        ObjectInputStream objectInputStream =
                new ObjectInputStream(new FileInputStream(c.pathDir+c.getPathTeams()));


        vList  = (ArrayList<Verein>) objectInputStream.readObject();

        for(int i = 0; i < vList.size();i++) {

            System.out.println(vList.get(i).getName()+" Anzahl Tore: "+vList.get(i).getAnzahltore());

        }

        objectInputStream.close();

        return vList;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

Result after first application Start entering test1 as teamName:

test1 Anzahl Tore: 0

test1 Anzahl Tore: 0

Result after reopen the application:

Anzahl Tore: 0

Anzahl Tore: 0

Content of of the file to read:

¬í sr java.util.ArrayListxÒ™Ça I sizexp w sr beans.VereinÏ7t¨r7!U xpsq ~ x

I have to solve the saving of the teams only with objectin-, objectoutputstream. Please only suggestions with objectin-, objectoutputstream.

Thanks a lot.

  • 4
    We're unlikely to be able to help you without an [mcve]. The details of the class and instance being written and of the code with which you're testing the readback both matter here. – John Bollinger Jan 21 '19 at 00:49
  • did you try to use flush() before closing? Is the object serialisable? – alsaleem Jan 21 '19 at 01:07
  • @alsaleem 1. `flush()` before `close()` is redundant. 2. If it wasn't `Serializable` there would have been a `NotSerializableException`, and no content in the file. – user207421 Jan 21 '19 at 01:10
  • @ScaryWombat No he isn't, and there was nothing in your duplicate that would help. – user207421 Jan 21 '19 at 01:12
  • @user207421 Yes, my bad. – Scary Wombat Jan 21 '19 at 01:20
  • please cast readObject to (ArrayList) instead of (ArrayList) according to this link https://howtodoinjava.com/java/collections/arraylist/serialize-deserialize-arraylist/ – alsaleem Jan 21 '19 at 01:35
  • @alsaleem Why? The generic cast is more correct, and more usable. The change you suggest will certainly not solve any problem. – user207421 Jan 21 '19 at 02:39
  • The class which reads and writes to the file implements Serializable. The Bean Class which should be write to a file and is read from it is implement Serializable too. – Sonic.Electronic Jan 21 '19 at 14:23
  • I simulated your code and you do not have problem with it. The only thing missing here is the "c.pathDir+c.getPathTeams()" check? – alsaleem Jan 22 '19 at 00:20

0 Answers0