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.