ObjectOutputStream for mass storage of objects.


My problem has to do with ObjectInputStream/ObjectOutputStream. This is a brief description of what I am trying to do.

I am writing a parser which is set to parse many large files. Each line in a file to parse is stored as a ParsedLine object. This is just a simple struct with get/set methods for setting/getting data. Anyway, because of the size and number of the files to parse a great many ParsedLine objects are being created. Instead of doing that I am creating just one ParsedLine object and letting the client retrieve a reference to this single object:

class Parser {
  private ParsedLine parsedLine;
  public void parse(String line) {'
    // clear old values in parsedLine here...
    // set new parsed values in parsedLine here...

  public ParsedLine() {
    return this.parsedLine;

Anyway, I want to store every ParsedLine object for later retreival. I am using an ObjectOutputStream for this:

FileOutputStream fileStream = new FileOutputStream(filename);
ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
ParsedLine copy = new ParsedLine(parsedLine);
objectStream = null;
copy = null;

However, although it seems every ParsedLine gets stored (the println writes the correct info) something is going wrong when reading. The retreival process only receives the last ParsedLine object stored. This is the reading process:

FileInputStream fileInput = null;
ObjectInputStream objectInput = null;
try {
  fileInput = new FileInputStream(args[0]);
  objectInput = new ObjectInputStream(fileInput);
  while (true) {
    try {
      ParsedLine line = (ParsedLine) objectInput.readObject();
      line = null;
    catch (EOFException e) {
catch (ClassNotFoundException e) {
  System.err.println("ClassNotFoundException " + e.getMessage());
catch (FileNotFoundException e) {
  System.err.println("FileNotFoundException " + e.getMessage());
catch (IOException e) {
  System.err.println("IOException " + e.getMessage());
finally {
  try {
    if (objectInput != null)
    if (fileInput != null)
  catch(IOException e) {
    System.err.println("IOException " + e.getMessage());

As I said this code only writes one line (using println) so only one line is read (only one line available in file?)

Can anyone see what am doing wrong? I think it might have to do with the fact that only one ParsedLine object is being created so a reference to that object is being written repeatadly, but I can't understand how. You will notice that I also have added a copy constructor for ParsedLine. I did that instead of using clone(). In any case I want to avoid creating too many ParsedLine objects because I am running out of memory (thats why I'm writing to disc).

Maybe this is the wrong usage of ObjectOutputStream/ObjectInputStream? (The reading object loop expects an exception to be thrown to terminate - this is not very good, but I couldn't find a better way).

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Seems you are just replacing the file each time you try to store new value, so it always contains only the last stored value. Try to open the file and ObjectOutputStream somewhere outside the storing method, so that the method uses same ObjectOutputStream each time it runs, and close the stream only after you are through with storing all of the values.

Good luck

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
boomerang061797Author Commented:
It's funny you should mention that. I actually had a code like that before. The only difference was that instead of getting only 1 instance I got the correct amount of ParsedLine, but all of the same ParsedLine value.

However, is there no way to append to the file? I tried to set append=true for the FileOutputStream but that didn't seem to work as expected.
Use constructor:
FileOutputStream(String filename, boolean append);

settin the second boolean argument to true opens file in append mode.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

boomerang061797Author Commented:
I tried that before as well. It caused an IO Exception when reading which was strange.

Is there an alternative way to store objects without using the ObjectOutputStream? I don't like the way I have to rely on the EOFException to stop reading the ObjectInputStream either. I get the feeling that this isn't the correct usage of the class.
>It caused an IO Exception when reading which was strange
Strange indeed, bet cause was not in appending, can you paste the code?

>Is there an alternative way to store objects without using the ObjectOutputStream

Well, ObjectOutputStream is very convenient as it handles serialization of all referenced objects as well, but if your object is simple enough you can write your own serialization/deserialization routines. An extreme example: doubt it's worth using ObjectOutputStream to write/read String objects to/from file.

>I don't like the way I have to rely on the EOFException to stop reading the ObjectInputStream

Only after reading this I realized that somehow I never have stored multiple instances of the same class in a single file, to be more precise when I need to store number of instances I put them into separate files if I'll need to load them under different circumstances OR, if I know I'll need to load them all at once, I put them into some collection and write that collection to the file. Of course that's matter of taste, but I never have to 'rely on the EOFException to stop reading'
Good luck
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Koka1's comment as answer.

Please leave any comments here within the next seven days.


EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Storage Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.