0

How to read the object in to the file i am use the ObjectInputStream and ObjectOutputStream class to read and write the Object of the my custom class Student for this demo.

Code to Write and read use::

            try
            {
                if(af.filepath==null || af.filepath=="")//file path
                {
                    JOptionPane.showMessageDialog(null, "Please Set File Path", "File Path Error", JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                    FileOutputStream fs=new FileOutputStream(af.filepath,true);
                    ObjectOutputStream fo=new ObjectOutputStream(fs);   
                    fo.writeObject(af.s);//write the Super Class Object
                    fo.close();
                }

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

   ---------------------------------------------------------------------------------
        try
        {
            if(af.filepath==null || af.filepath=="")//file path have whole path of the file
            {
                JOptionPane.showMessageDialog(null, "Please Set File Path", "File Path Error", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                Student sp;
                FileInputStream fs=new FileInputStream(af.filepath);
                ObjectInputStream fo=new ObjectInputStream(fs);
                while ((sp=(Student)fo.readObject())!=null) 
                {
                    sp.set();//for print object
                }
                fo.close();
            }

        }
        catch (Exception ex)
        {
                ex.printStackTrace();
        }

using this i am read first object in the file but after that raise an error

Hiren Raiyani
  • 754
  • 2
  • 12
  • 28
  • which error rises paste error here – Engineer Oct 21 '13 at 08:04
  • 1
    java.io.StreamCorruptedException: invalid type code: AC at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at MyPackage.MyCustomListener.actionPerformed(AddStudentFrame.java:171) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19 95) – Hiren Raiyani Oct 21 '13 at 08:05
  • visit this [link](http://stackoverflow.com/questions/2393179/streamcorruptedexception-invalid-type-code-ac) posible it will be helpfull – Engineer Oct 21 '13 at 08:12

1 Answers1

1

You have written one object and you are attempting to read multiple objects. When you attempt to read the second one, you are bound to get an exception.

If you want to read back an indefinite number of objects ... like that ... I suggest you write a null to the stream:

    fo.writeObject(null);

(The javadoc doesn't say you can do this, but the Java Object Serialization spec says you can; see http://docs.oracle.com/javase/7/docs/platform/serialization/spec/output.html#933 ... step 3.)


The other problem (and this is what causes the corruption) is that you are attempting to append serialized objects to an existing file. That's not going to work. The serialization protocol says that a stream consists of a header followed by zero or more serialized objects ... and then the end of file. If you append one stream to another (e.g. FileOutputStream(path, true), the extra header is going to make the combined file readable at the point where the appended stuff starts.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I am write multiple object not only single object when i am fetching then when read pointer reached at second object then raise an error – Hiren Raiyani Oct 21 '13 at 08:15