1 2 Previous Next 28 Replies Latest reply on Apr 19, 2007 12:03 PM by 807606

    java.io.StreamCorruptedException

    807606
      hi all,


      File file = new File("logInfo.log");
      FileInputStream fileInput = new FileInputStream(file);
      ObjectInputStream objectInput = new ObjectInputStream(fileInput);

      am tried this code but, it throws java.io.StreamCorruptedException

      what is the reason and what i do?

      help me
        • 1. Re: java.io.StreamCorruptedException
          807606
          Err, "logInfo.log" doesn't sound like a file that would have serialized objects in it.

          Does it have serialized objects in it?
          • 2. Re: java.io.StreamCorruptedException
            807606
            yes, like this

            14.06.41 INFO [server.main.Main] = com.ct.common.impl.AuditRecordImpl@43302cee

            the above lines from the file "logInfo.log"

            how can i debug that one, help me...
            • 3. Re: java.io.StreamCorruptedException
              807606
              I think you are not creating "logInfo.log" file properly(ie, serializing)Refer this eg to serialize and deserialize.
              import java.io.FileInputStream;
              import java.io.FileNotFoundException;
              import java.io.FileOutputStream;
              import java.io.IOException;
              import java.io.ObjectInputStream;
              import java.io.ObjectOutputStream;
              
              public class TestSerialize {
              
                   public TestSerialize() {
                        super();
                   }
              
                   public static void main(String[] args) {
              
                        String fileName = "C:/logInfo.log";
                        String person = new String("TestString");
                        FileOutputStream fos = null;
                        ObjectOutputStream oos = null;
                        FileInputStream fis = null;
                        ObjectInputStream ois = null;
              
                        try {
                             
                             
                             
                             fos = new FileOutputStream(fileName);
                             oos = new ObjectOutputStream(fos);
                             System.err.println("Writing object....");
                             oos.writeObject(person);
                             oos.close();
                             
                             fis = new FileInputStream(fileName);
                             ois = new ObjectInputStream(fis);
                             String newPerson = null;
                             newPerson = (String) ois.readObject();
                             System.err.println(newPerson.toString());
                             ois.close();
                             
                        } catch (FileNotFoundException e) {
                             e.printStackTrace();
                        } catch (IOException e) {
                             e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                             e.printStackTrace();
                        }
                   }
              }
              • 4. Re: java.io.StreamCorruptedException
                807606
                check this out
                import java.io.*;
                
                public class WriteObjectToFile {
                     
                     public static void main(String [] arg) {
                          WriteObjectToFile wotFile = new WriteObjectToFile();
                          wotFile.WriteToFile();
                          wotFile.ReadFromFile();
                     }
                     
                     private boolean WriteToFile(){
                          try{
                               File file = new File("abc.txt");
                               FileOutputStream foutStream = new FileOutputStream(file);
                               ObjectOutputStream ooutStream = new ObjectOutputStream(foutStream);
                               UserInfo uInfo = new UserInfo(1, "some name");
                                    ooutStream.writeObject(uInfo);
                                    ooutStream.flush();
                          }catch(Exception e){
                               e.printStackTrace();
                          }
                          return true;
                     }
                     private boolean ReadFromFile(){
                          try{
                               File file = new File("abc.txt");
                               FileInputStream finStream = new FileInputStream(file);
                               ObjectInputStream oinStream = new ObjectInputStream(finStream);
                               UserInfo uInfo = null;
                               do{
                                    uInfo = (UserInfo)oinStream.readObject();
                                    System.out.println(uInfo.getId());
                               }while(oinStream.available() > 0);
                          }catch(Exception e){
                               e.printStackTrace();
                          }
                          return true;
                     }
                
                }
                
                class UserInfo implements Serializable{
                     private String strName;
                     private int id;
                     
                     public UserInfo(int id, String name) {
                          this.strName = name;
                          this.id = id;
                     }
                     
                     public String getName(){
                          return this.strName;
                     }
                     
                     public int getId(){
                          return this.id;
                     }
                }
                • 5. Re: java.io.StreamCorruptedException
                  807606
                  14.06.41 INFO [server.main.Main] =
                  com.ct.common.impl.AuditRecordImpl@43302cee
                  the above lines from the file "logInfo.log"
                  The reason you are getting java.io.StreamCorruptedException is. You are trying to read non serialized file( "logInfo.log")
                  • 6. Re: java.io.StreamCorruptedException
                    807606
                    I think the object which is being stored should be serialized.
                    • 7. Re: java.io.StreamCorruptedException
                      807606
                      You're trying to read objects from a log file that shows the output pf the default toString() implementation?

                      Very classy :)
                      • 8. Re: java.io.StreamCorruptedException
                        807606
                        i tried your code also:
                        but it throws same Exception


                        and also i tried to created Inter Class

                        like
                        ..
                        .
                        .
                        .
                                       try
                                       {
                                            File file = new File("C:/projects/ciritech/ems/logInfo.log");
                                            FileInputStream finStream = new FileInputStream(file);
                                            ObjectInputStream oinStream = new ObjectInputStream(finStream);
                                            AuditClass aInfo = null;
                                            do
                                            {
                                                 aInfo = (AuditClass)oinStream.readObject();
                                                 System.out.println(aInfo.getAttributes());
                                            }
                                            while(oinStream.available() > 0);
                                       }
                                       catch(Exception e){System.out.println(e);}
                        .
                        .
                        .
                        .
                        .
                        .

                             private class AuditClass implements Serializable
                             {
                                  private String name;
                                  private String attr;

                                  public AuditClass()
                                  {

                                  }

                                  public AuditClass(String attr, String name)
                                  {
                                       this.name = name;
                                       this.attr = attr;
                                  }

                                  public String getName()
                                  {
                                       return this.name;
                                  }

                                  public String getAttributes()
                                  {
                                       return this.attr;
                                  }


                             }




                        please Help me guys!!!...
                        • 9. Re: java.io.StreamCorruptedException
                          807606
                          how you are writing to the file ?

                          You can read only that serialized object which was previously stored.

                          Storing one object and trying to read another does not make sense.

                          The code which I posted is a working example.
                          • 10. Re: java.io.StreamCorruptedException
                            807606
                            As i said before, Serialize object and write it to file Before reading
                            • 11. Re: java.io.StreamCorruptedException
                              807606
                              please post in one thread
                              • 12. Re: java.io.StreamCorruptedException
                                807606
                                Here is what you have to do
                                try {
                                     AuditClass auditClass = new AuditClass();
                                     File file = new File("C:/projects/ciritech/ems/logInfo.log");
                                
                                     FileOutputStream fos = new FileOutputStream(file);
                                     ObjectOutputStream oos = new ObjectOutputStream(fos);
                                     oos.writeObject(auditClass);
                                
                                     FileInputStream finStream = new FileInputStream(file);
                                     ObjectInputStream oinStream = new ObjectInputStream(finStream);
                                     AuditClass aInfo = null;
                                     do {
                                          aInfo = (AuditClass) oinStream.readObject();
                                          System.out.println(aInfo.getAttributes());
                                     } while (oinStream.available() > 0);
                                } catch (Exception e) {
                                     System.out.println(e);
                                }
                                • 13. Re: java.io.StreamCorruptedException
                                  807606
                                  Sir,

                                  am using log4J to created the "logInfo.log" file

                                  after that i use the file concept
                                  file contain like
                                  14.06.41 INFO [server.main.Main] = com.ct.common.impl.AuditRecordImpl@43302cee
                                  14.18.25 INFO [server.main.Main] = com.ct.common.impl.AuditRecordImpl@b595f812
                                  14.23.13 INFO [server.main.Main] = com.ct.common.impl.AuditRecordImpl@16a30462
                                  15.23.37 INFO [server.main.Main] = com.ct.common.impl.AuditRecordImpl@39ca0340


                                  here my target is read that file , collect the attribute (from the Object) and display it.
                                  • 14. Re: java.io.StreamCorruptedException
                                    807606
                                    here my target is read that file , collect the
                                    attribute (from the Object) and display it.
                                    Then you will have to use DataInputStream. read every line as string and proceed with your reqirement
                                    Here is the sample code!
                                    public static void main(String[] args) {
                                         File file = new File("C:/projects/ciritech/ems/logInfo.log");
                                         FileInputStream fis = null;
                                         DataInputStream dis = null;
                                         try {
                                              fis = new FileInputStream(file);
                                              dis = new DataInputStream(fis);
                                              String str = "";
                                              while ((str = dis.readLine()) != null) {
                                                   System.err.println(str); // collect the attribute (from the str) and display it.           }
                                         } catch (FileNotFoundException e) {
                                              e.printStackTrace();
                                         } catch (IOException e) {
                                              e.printStackTrace();
                                         } finally {
                                              try {
                                                   if (dis != null) {
                                                        dis.close();
                                                   }
                                                   if (fis != null) {
                                                        fis.close();
                                                   }
                                              } catch (IOException e) {
                                                   e.printStackTrace();
                                              }
                                         }
                                    
                                    }
                                    1 2 Previous Next