Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

ObjectOutputStream for mass storage of objects.

Hi

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.writeObject(copy);
System.out.println(parsedLine);
objectStream.flush();
objectStream.close();
     
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();
      System.out.println(line);
      line = null;
    }
    catch (EOFException e) {
      break;
    }
  }
}
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)
      objectInput.close();
    if (fileInput != null)
      fileInput.close();
  }
  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).

Suggestions?
0
boomerang061797
Asked:
boomerang061797
3 Solutions
 
Koka1Commented:
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
0
 
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.
0
 
Koka1Commented:
Use constructor:
FileOutputStream(String filename, boolean append);

settin the second boolean argument to true opens file in append mode.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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.
0
 
Koka1Commented:
>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
0
 
CleanupPingCommented:
boomerang:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
TimYatesCommented:
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now