java serialization and deserializaion -


i have created simple program serializes string input cmd .ser file.. part of requirement program must able append new input , able read new input plus old input.. streamcorruptedexception if read after 2nd input..

here run on cmd.. how solve streamcorruptedexception , why happen??. codes given below.

c:\users\msi\desktop\codes java>java writefile cc.ser enter text , press ^z or ^d end. hah haha hahaha try ^z  c:\users\msi\desktop\codes java>java writefile cc.ser enter text , press ^z or ^d end. asd asd asd asd asd ^z  c:\users\msi\desktop\codes java>java readfile cc.ser 1: haha 2: haha 3: hahaha  4: hahaha error : java.io.streamcorruptedexception: invalid type code: ac     @ java.io.objectinputstream.readobject0(objectinputstream.java:1375)     @ java.io.objectinputstream.readobject(objectinputstream.java:370)     @ readfile.main(readfile.java:23) 

writefile.java:

 import java.io.*;  public class writefile implements java.io.serializable  { public static void main(string args[])       {            try         {            file myfile = new file(args[0]);             bufferedreader br = new bufferedreader                                           (new inputstreamreader(system.in));                objectoutputstream oos = new objectoutputstream                                           (new fileoutputstream(myfile,true));             system.out.println("enter text , press ^z or ^d end.");             string str;             while ((str = br.readline()) != null)              {                      oos.writeobject(str);             }              br.close();            oos.close();         }          catch (ioexception i)         {             i.printstacktrace();         } }} 

readfile.java:

import java.io.*;     public class readfile     {        public static void main(string args[])     {          try         {         int ctr = 0;          file myfile = new file(args[0]);              objectinputstream ois = new objectinputstream                                                 (new fileinputstream( myfile ));          string str;                 while ((str = (string)ois.readobject()) != null)              {                    system.out.println(++ctr + ": " + str);              }              ois.close();                 }          catch (eofexception ex)         {           system.out.println("\nend of file reached ");         }            catch (classnotfoundexception c)         {           system.out.println("the error : ");           c.printstacktrace();         }catch (ioexception i)         {           system.out.println("the error : ");           i.printstacktrace();         }      }} 

this exception occurs whenever u trying create new outputstream object existing input stream/trying read before written in case ,the control information read object stream violates internal consistency checks.

use single oos , ois life of socket, , don't use other streams on socket.

also u might want implement same using threads in same program.

if want forget you've written, use objectoutputstream.reset().


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -