Originally Posted by
Panoksa
...
1. At the moment everytime I write new information into a file, my old info is gone.How can I modify my code so everytime I write a new info into a file it will be saved onto a new line.
Here's the thing: ObjectOutputStream objects are not designed to be appendable. Reference the
ObjectOutputStream API
I mean, you can open the underlying FileOutputStream in appendable mode, but when you create an ObjectOutputStream object the constructor immediately writes a new Serializable header to the stream, which results in a corrupted stream at that point (the end of the old file) when you append things and then, later, try to read in the new stuff.
At least that what I observed. (I'm new to Java, and I would welcome any enlightenment here. It wouldn't be the first time I overlooked something that is "easy and obvious" to anyone with more than four months experience.)
I keep thinking that they could have made the ObjectInputStream readObject() "smarter," to get past this, but maybe that wasn't a good idea either. Maybe there is a Good Reason why things are as they are.
Anyhow...
Maybe you can read all of the previous stuff from the file and then create a brand new file and ObjectOutputStream and write the old stuff and then the new stuff. You can write as many objects as you want in a single session with a single new file, right? (By packaging the objects in a collection, I'm thinking. Maybe an ArrayList or some such thing?)
There is some discussion about a way to create appendable ObjectOutputStream objects is here:
http://stackoverflow.com/questions/1...38141#12438141 I haven't tried it (because, for one thing, I couldn't think of a bullet-proof way of knowing how much stuff to read), but those guys always seem to be really on top of things.
Originally Posted by
Panoksa
2....Everytime I read the info from file created it shows something like
System.out.println(se.getElChoice());
.
.
[Ljava.lang.String;@5456a499
Your object's elchoice is an array of Strings. If you use something like
System.out.println(arrayName) for any kind of array, you get the name of the array and a hashcode. To print the
contents of an array in a single print statement, you can use the Arrays static
toString() method:
(You might want to change the
print() method in your StudentElective class to something like this also.)
Cheers!
Z