0

I am dumping an arraylist of object into a file.I am serialising object by object and logging into a file instead of whole arraylist serialisation because no. of arraylist can be mor than one. The process is working fine and data is appending in the file but during the deserialisation i can retrieve only first arraylist objects and as soon as second arraylist objects come it throws java.io.StreamCorruptedException: invalid type code: AC . My each arraylist have same type of objects. I have tried to search a lot but none of the tricks like reset, AppendableObjectoutputstream are working.

My serialising code is:

 public static void dumplogInFile(ArrayList<HtmlElementObject> sanitizelog)
{
    try
      {
         String fileName = "c:\\temp\\ auditinfo.ser";
         FileOutputStream fileOut =
         new FileOutputStream(fileName , true);
         ObjectOutputStream  out = new ObjectOutputStream(fileOut);
         for(HtmlElementObject eachobj : sanitizelog)
         {
             out.writeObject(eachobj);
             out.reset() ;
         }
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/auditinfo.ser");
         //logElementData.clear();
      }catch(IOException i)
      {
          i.printStackTrace();
      }

}

here number of arraylist will be more instead of one as it works like that if webservice will fail then it will dump the data in file.

My Deserialising Code to read data from file

 public static ArrayList<HtmlElementObject> extractLogFromFile()
    {

        HtmlElementObject v ;
        ArrayList<HtmlElementObject> extractLogList = new ArrayList<HtmlElementObject>();


        try
          {
             FileInputStream fileIn = new FileInputStream("c:\\temp\\ auditinfo.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn);
          //   ObjectInputStream in = new ObjectInputStream(fileIn);
           /*  ArrayList<HtmlElementObject> arr = (ArrayList<HtmlElementObject>) in.readObject();

           //  for (HtmlElementObject a : arr) 
             //   {
                 //a = (HtmlElementObject) in.readObject();
                 //extractLogList.add(a);

                //}*/

             int count =0;
             while( (v = (HtmlElementObject)in.readObject()) != null)
             {
                 System.out.println(count++);

                 HtmlElementObject a;
                 a = v;//(HtmlElementObject) in.readObject();
                 extractLogList.add(a);


             }


           in.close(); 
           fileIn.close();
          }catch(IOException i)
          {
             i.printStackTrace();
             return extractLogList ;
          }catch(ClassNotFoundException c)
          {
             System.out.println(" class not found");
             c.printStackTrace();
             return null ;
          }
        return extractLogList;
    }

}

Now this function will easily read all the objects of first arraylist but as the new arraylist object will come then it throws java.io.StreamCorruptedException.

Piyush
  • 388
  • 1
  • 6
  • 21

1 Answers1

0

You can't append to object stream files without a special trick to suppress the second stream header which is written by the constructor of ObjectOutputStream. Your ObjectInputStream is encountering that where it expects an object, and barfing.

user207421
  • 305,947
  • 44
  • 307
  • 483