OK, so I want to thank all of you that helped me with my little problems within the past few days,I have almost come to the end of this project thanks to your suggestions. Now the thing is, I used ObjectInputStream & ObjectOutputStream to write down whole objects to a textual file and later read from the file, and it works perfectly, but it only shows me the latest entries. That is, when I write an object to the file,then terminate the program, and later start another program that reads from that file, I only get the object that I wrote the last time, not the ones I wrote down in the previous attempts. So I do not know if the problem is in my code logic, or am I misusing the ObjectInputStream & ObjectOutputStream methods? Will you please take a look at my code snippet? Thanks a lot,regards.
This "result" object is an instance of the Result class, a class that I created in order to store the results from the voting as integers, then write them to this object and then write the object to the file. This "addBand()" method takes the variables that represent the results, then just stores them in its own fields, and I write the whole object down.
result.addBand(zepp, stones, floyd, purple, acDc);
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("votingResults.txt"));
stream.writeObject(result);
stream.close();
Then I have this method for reading from the file. Anyway, I assume that the following code is OK, since it does read correctly. I assume that I've made a mistake in writing to the file, since it never gets bigger - it's only 1KB always!
public void readStream(){
ObjectInputStream inputStream = null;
try{
inputStream = new ObjectInputStream(new FileInputStream("votingResults.txt"));
Object obj = null;
while(!(obj = inputStream.readObject()).equals(null)){
if(obj instanceof Result){
System.out.println(((Result)obj).toString());
}
}
inputStream.close();
}catch(EOFException eof){
System.out.println("End of file reached");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}