How to read objects from file in a loop


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: How to read objects from file in a loop

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Posts
    7

    Exclamation How to read objects from file in a loop

    Hi,
    I am trying to read object of perticular type into a data structure from a file. I have written objects into this file earlier. Below is the code I am using to write to file

    public void CreateOrg(Organisation creatingOrg)

    {

    FileOutputStream fileOut,fileOut2;
    ObjectOutputStream objOutStrm,objOutstrm2;
    //FileInputStream fileIn;
    //ObjectInputStream oInStrm;

    try
    {
    fileOut = new FileOutputStream(creatingOrg.name+".org");
    objOutStrm= new ObjectOutputStream(fileOut);
    objOutStrm.writeObject(creatingOrg);
    objOutStrm.flush();
    objOutStrm.close();
    // file containing list of available organisation
    fileOut2= new FileOutputStream("Orglist.lekha",true);
    objOutstrm2= new ObjectOutputStream(fileOut2);
    objOutstrm2.writeObject(creatingOrg);
    objOutstrm2.flush();
    objOutstrm2.close();

    }
    catch(Exception e)
    {
    System.out.println("I/O ERROR");
    }

    }

    here creatingOrg is a object of type Organisation class and this class is serializable.

    and else where I am trying to read from the file "Orglist.lekha". Below is the code I am using to read back the objects

    void loadOrgList()

    {
    String currOrg;
    FileInputStream inFile;
    ObjectInputStream inObj;
    boolean flag=false;
    Organisation temp= new Organisation();
    try
    {
    inFile= new FileInputStream("Orglist.lekha");
    inObj= new ObjectInputStream(inFile);
    while(!flag)
    {
    System.out.println("Inside While");
    System.out.println(flag);
    try{
    temp= (Organisation)inObj.readObject();
    System.out.println(temp.name);
    }
    catch(EOFException eof){System.out.println(eof); flag=true;}

    }
    }
    catch(Exception e){ System.out.println(e);}

    //catch(ClassNotFoundException clss){System.out.println(clss);}
    }

    Now my problem is that when I try to read objects continously, I am able to read only the first written object. While writing "Orglist.lekha" append= true. so even if I have written more than one object I am not able to read it back. I am getting following error in my second run of the while() loop. Please help me with this because I am stuck here and my work is not proceeding.

    Thank you and greatful any one how cares to read this
    bye

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468

    Objects from file in a loop

    Have you looked at a more direct test as the while test, rather than the flag?

    The ObjectInputStream has a read() method which returns an integer representation of the next available byte - which returns -1 if you have reached the end of the file. Now, as long as when you read the next byte you do not discard that next byte, you can test for -1 at the top of the loop, then do a readObject() to get the next object, return to the top of the loop and test for the end again, then read another Object if you can, etc.

    As an alternative you might be able to use the available() method to tell you that you still have stuff in the input buffer:
    Code:
    while ( inObj.available() != 0 ) 
    { 
         Organisation temp = new Organisation();
         temp = (Organisation)inObj.readObject();
         dataStore.add(temp); OR
         System.out.println( "The new organisation is " + temp );
    }
    or something like that ...

  3. #3
    Join Date
    Feb 2005
    Posts
    7
    Thanks for the quick reply
    I tried what you have suggested but it is again giving me StreamCorruptedException. First it is not going in side the while loop, so I tried do while loop. here it reads the first record and then duing the second run of loop it is giving me the error. Can you please suggest me some other way to acheive the required task.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    At the end of the first loop, after you have retrieved your first object, how many bytes remain in your ObjectInputStream?

    What happens if you break your "read" process into two separate "try/catch" blocks, rather than one nested in the other?

  5. #5
    Join Date
    Feb 2005
    Posts
    7
    Even at the first run of loop or even simply read() or available() returns -1 and 0 respectively. Tried using two seperate try catch blocks no avail. Do you think I am making mistake while writing objects.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468

    Objects from file

    Yup ... sounds like you have nothing in your stream. Are you sure you have something in the file to begin with?

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Just for the record....

    Code:
    while ( inObj.available() != 0 ) { 
         Organisation temp = new Organisation(); //<--------- why ?
         temp = (Organisation)inObj.readObject(); // the temp above is gone here....
         dataStore.add(temp); OR
         System.out.println( "The new organisation is " + temp );
    }
    eschew obfuscation

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468

    Objects from file in a loop

    The declaration is there because I had forgotten that he had declared temp in the code already, before the loop ...

    But I would have put the declaration inside the loop if this was, indeed, a temporary object

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Quote Originally Posted by nspils
    The declaration is there because I had forgotten that he had declared temp in the code already, before the loop ...

    But I would have put the declaration inside the loop if this was, indeed, a temporary object
    I meant, why the new stuff, like:
    Code:
    Organisation temp = new Organisation();
    temp = (Organisation)inObj.readObject();
    when you might as well just write:

    Code:
    Organisation temp = (Organisation)inObj.readObject();
    eschew obfuscation

  10. #10
    Join Date
    Feb 2005
    Posts
    7
    Hi friends,
    First of all thank you for all the people, I have been working on this problem for all most a week from now. And I have come up with an answer. It seem that ObjectStream in java does not allow to read objects written into a file as I have done. Please refer to my first post. So i figured out that if it does not allow to read multiple objects why not use only one object, and let this object contain all the other objects.
    One such implementation would be to use vectors.
    What I did is I declared a vector and then I inserted all the objects I wanted to write to file into this vector. After this I wrote this vector object to file. While reading it back I read it to a vector. By this I was able to solve my problem.
    thank you
    bye

  11. #11
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468

    Objects from file in a loop

    Good work! Way to stay with it and thinking to look at the problem from another angle ...!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center