Results 1 to 4 of 4
  1. #1
    sakshi is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default error in reading objects from file

    Below is a short code to explain the problem i am facing
    i have been working on the same for past one week i am facing exceptions while reading a file containing more than one objects

    Java Code:
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author sakshi
     */
    public class Storingobject implements Serializable{
        int x,y;
        
        
        public void addObject()
        {
            //Storingobject temp;
            ObjectOutputStream objOut=null;
            try {
                objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("temp.dat")));//if i pass true as parameter in FileOutputStream it gives Stream Corrupted exception
            } catch (IOException ex) {
                Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                // while((temp=objOut.writeObject(o))!=null)
                objOut.writeObject(this);
                System.out.println("Saved..");
                objOut.close();
            } catch (IOException ex) {
                Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex);
            }
               
            
        }
        
        public void readobject()
        {
            int i=1;
            Storingobject temp;
            ObjectInputStream objIn=null;
            try {
                objIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("temp.dat")));
            } catch (IOException ex) {
                Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                while ((temp = (Storingobject) objIn.readObject()) != null) {
                    System.out.println("VAlues of object i++ :");
                    System.out.println("X: "+temp.x+"Y: "+temp.y);
                }
            } catch (IOException ex) {
                Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex);
            }
        
            
            
        }
     
        
        public static void main(String args[])
        {
            Storingobject o1=new Storingobject();
            o1.x=10;o1.y=15;
            o1.addObject();
            o1.readobject();
            
        }
    }
    please i would be grateful if u could reply as early as possible

  2. #2
    Join Date
    Jul 2009
    Posts
    10
    Rep Power
    0

    Default

    i modified your main method:
    Java Code:
    public static void main(String args[]) {
            StoringObject o1 = new StoringObject();
            o1.x = 10;
            o1.y = 15;
            o1.addObject();
            o1.readobject();
            
            StoringObject o2 = new StoringObject();
            o2.x = 20;
            o2.y = 25;
            o2.addObject();
            o2.readobject();
        }
    there are two objects, but i found no error.
    -----------------------------
    executes everything

  3. #3
    Join Date
    Jul 2009
    Posts
    10
    Rep Power
    0

    Default reply

    i modified your main method:
    Java Code:
    public static void main(String args[]) {
            StoringObject o1 = new StoringObject();
            o1.x = 10;
            o1.y = 15;
            o1.addObject();
            o1.readobject();
            
            StoringObject o2 = new StoringObject();
            o2.x = 20;
            o2.y = 25;
            o2.addObject();
            o2.readobject();
        }
    there are two objects, but i found no error.
    -----------------------------
    executes everything

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    StreamCorruptedException? odd, I would have though the constructors would be safe from that, but apparently not. Rather unlikely that it has anything to do with the filename, but try just in case the .dat extension is causing problems for the constructor with a boolean.

    Try a static variable to track number of objects written. i.e
    Java Code:
    private static int numObjs=0;
    public void addObject(){
         StoredObject[] orig = new StoredObject[numObjs];
         //read objects and store in an array
         try {
                objIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream("temp.dat")));
            } catch (IOException ex) {
                Logger.getLogger(Storingobject.class.getName()).log(Level.SEVERE, null, ex);
            }
         if(objIn!=null){
              for(int i=0;i<numObjs;++i){
                   orig[i]=(StoredObject) objIn.readObject();
                   //check if object read is null and do error handling to solve problem for later writes
                   if(orig[i]==null){
                        numObjs=i - 1;
                   }
              }
         }
         //write objects to file (iterate through the array using another for loop)
         //then write the new object
         ++numObjs;
    }
    Code is untested and may have errors
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  2. Error Message when reading an input file.
    By Deluyxe in forum New To Java
    Replies: 8
    Last Post: 04-26-2009, 05:02 PM
  3. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  4. error in reading mail contents
    By kib_tse in forum Networking
    Replies: 1
    Last Post: 08-22-2008, 01:14 AM
  5. reading from a zip file, error
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2008, 01:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •