ObjectOutputStream for mass storage of objects.

Posted on 2003-02-28
Medium Priority
Last Modified: 2013-11-15

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).

Question by:boomerang061797
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Koka1 earned 200 total points
ID: 8044920
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

Author Comment

ID: 8046790
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.

Assisted Solution

Koka1 earned 200 total points
ID: 8047305
Use constructor:
FileOutputStream(String filename, boolean append);

settin the second boolean argument to true opens file in append mode.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


Author Comment

ID: 8047475
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.

Assisted Solution

Koka1 earned 200 total points
ID: 8053240
>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

Expert Comment

ID: 9059013
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.
LVL 35

Expert Comment

ID: 9677756
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question