Append Objects to a ObjectOutput Stream

i have opened a FileOutputStream(fis)(name,true) for appending and created a ObjectOutputStream with above fis. eventhough i am writing objects into file while reading i am only getting first object then i am getting StreamCorruptedException. why i am getting that exception and how to overcome my problem so that i can able to append object to existing file which holds objects?

In the append mode, the file header is writted twice (it seems to be a bug...)...
You must override the ObjectInputStream and the ObjectOutputStream to avoid the problem.
public class MyObjectOutputStream extends ObjectOutputStream {
public MyObjectOutputStream(OutputStream out) throws IOException {
super(out);
protected void writeStreamHeader() throws IOException {
// Nothing in header
public class MyObjectInputStream extends ObjectInputStream {
public MyObjectInputStream(InputStream in) throws IOException, StreamCorruptedException{
super(in);
protected void readStreamHeader() throws IOException, StreamCorruptedException{
// Nothing in header
}

Similar Messages

  • Write updated Object with ObjectOutput Stream over a socket

    Hello,
    i would like to use an ObjectOutput Stream ot write over a socket between 2 process. But each time the object is modified on a given process i would like to resend it to the other in order to get the object updated.
    My porblem is that the object is never retrieved in its modified state on the second process.
    here is the class that i'm using to send and receive objects.
    On the server it is instanciated with serverMode=true and false on the client
    public class GameClient extends Thread
         private String m_servername="";
         private int m_serverport=0;
         private ObjectOutputStream m_oos = null;
         private ObjectInputStream m_ois = null;
         private Socket m_socket = null;
         private ServerSocket m_serverSocket = null;
         private Vector<INetworkController> m_listeners = new Vector<INetworkController>();
         private static final Trace trace = new Trace("SimpleClient");
         private boolean m_serverMode=false;
         public GameClient(String serverName,int port,boolean serverMode) throws IOException, UnknownHostException
              try
                   m_serverMode = serverMode;
                   m_servername=serverName.trim();
                   m_serverport=port;
                   if (m_serverMode)
                        m_serverSocket = new ServerSocket( m_serverport);
                   else
                        m_socket = new Socket(m_servername, m_serverport);
                        m_oos = new ObjectOutputStream(m_socket.getOutputStream());
                        m_ois = new ObjectInputStream(m_socket.getInputStream());
                   start();
              catch (BindException e)
                   e.printStackTrace();
                   String message = "Impossible de lancer le serveur. Un autre process �coute d�j� sur ce port";
                   System.exit(0);
         public void run()
              try
                   if (m_serverMode)
                        Socket client = m_serverSocket.accept();
                        m_oos = new ObjectOutputStream(client.getOutputStream());
                        m_ois = new ObjectInputStream(client.getInputStream());
              catch(Exception e)
                   e.printStackTrace();
              trace.startMethod("run()");
              trace.println("Listening Client Thred started");
              while(true)
                   try
                        Event serverMessage = (Event) m_ois.readObject();
                        trace.println("Waiting for message");
                        dispatchEvent(serverMessage);
                        trace.println("Message Reveived");
                   catch(Exception e)
                        String message = "La connexion r�seau a �t� perdue";
                        e.printStackTrace();
                        trace.endMethod();
                        try
                             if(m_socket!=null)
                                  m_socket.close();
                             if (m_serverSocket!=null)
                                  m_serverSocket.close();
                             m_oos.close();
                             m_ois.close();
                        catch (Exception e2)
                             e2.printStackTrace();
                        return;
         private int m_counter=0;
         public void sendNetworkMessage(Event msg)throws IOException, UnknownHostException
              if (m_oos != null)
                   m_oos.writeObject(msg);     
                   m_oos.flush();
                   //m_oos.reset();
              time = System.currentTimeMillis()-time;
              trace.endMethod();
         }Each time an object can be read, the read object is dispatch with the method dsiaptchEvent() which is not described here...
    I do not inderstand what's wrong in here
    Edited by: Voldor on Oct 16, 2007 4:10 PM

    Thanks but if i uncomment m_oos.reset() each time i write something on the socket (on the client or on the server) it is more and more longer to retrieve the writeen object on the other side of the socket (more than 40 sec after 12 messages exchanged, 6 coming from the server and 6 form the client)
    and after all i got the following
    java.lang.ArrayIndexOutOfBoundsException
         at java.net.SocketInputStream.socketRead0(Native Method)
         at java.net.SocketInputStream.read(Unknown Source)
         at java.net.SocketInputStream.read(Unknown Source)
         at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
         at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
         at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readArray(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
         at java.io.ObjectInputStream.readSerialData(Unknown Source)
         at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
         at java.io.ObjectInputStream.readObject0(Unknown Source)
         at java.io.ObjectInputStream.readObject(Unknown Source)
         at dt.communication.GameClient.run(GameClient.java:78)Do you have any idea ? Any recommandation about when to call reset ?
    Edited by: Voldor on Oct 16, 2007 4:44 PM

  • How to append Objects in a file.

    i have the following sample code ,
    its not read data properly and throws Stream Corrupted Exception in the appended record. ( at Last statement of try block ).
    Thanks,
    import java.io.*;
    public class IOError {
    public static void main(String[] args) {
    try {
    ObjectOutputStream oos = new ObjectOutputStream(
    new FileOutputStream("data.dat"));
    oos.writeObject(new String("string 1"));
    oos.writeObject(new String("string 2"));
    oos.flush();
    oos.close();
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.dat"));
    System.out.println("----- Round 1 Starts ---------");
    System.out.println((String) ois.readObject());
    System.out.println((String) ois.readObject());
    ois.close();
    ObjectOutputStream oos2 = new ObjectOutputStream(
    new FileOutputStream("data.dat",true)); // Appending Data
    oos2.writeObject(new String("string 3"));
    oos2.flush();
    oos2.close();
    ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream("data.dat"));
    System.out.println("----- Round 2 Starts ---------");
    System.out.println((String) ois2.readObject());
    System.out.println((String) ois2.readObject());
    System.out.println((String) ois2.readObject()); // Stream Corrupted Exception
    catch(Exception e) {
    System.out.println(e);

    Nasty. When you open an ObjectOutputStream, it will write a few initializing bytes, so if you really want to append objects to a file, then after you read the first two strings, you need to create a new ObjectInputStream that will read those bytes.
    You could write an object indicating that now you are done with this ObjectOutputStream, so when you read the file and read this object, you know you have to create a new ObjectInputStream.
    oos.writeObject(new String("string 1"));
    oos.writeObject(new String("string 2"));
    oos.writeObject(null); // null could be used if you know you won't write any other null's in your code.
    //... append
    oos.writeObject(new String("string 3"));
    oos.writeObject(null);
    // read:
    FileInputStream fis = new FileInputStream("data.dat");
    ObjectInputStream ois2 = new ObjectInputStream(fis);
    String s;
    while ((s = (String)ois2.readObject()) != null) {
      System.out.println(s);
    ois2 = new ObjectInputStream(fis);
    while ((s = (String)ois2.readObject()) != null) {
      System.out.println(s);

  • How to read appended objects from file with ObjectInputStream?

    Hi to everyone. I'm new to Java so my question may look really stupid to most of you but I couldn't fined a solution by myself... I wanted to make an application, something like address book that is storing information about different people. So I decided to make a class that will hold the information for each person (for example: nickname, name, e-mail, web address and so on), then using the ObjectOutputStream the information will be save to a file. If I want to add a new record for a new person I'll simply append it to the already existing file. So far so good but soon I discovered that I can not read the appended objects using ObjectInputStream.
    What I mean is that if I create new file and then in one session save several objects to it using ObjectOutputStream they all will be read with no problem by ObjectInputStream. But after that if in a new session I append new objects they won't be read. The ObjectInputStream will read the objects from the first session after that IOException will be generated and the reading will stop just before the appended objects from the second session.
    The following is just a simple test it's not actual code from the program I was talking about. Instead of objects containing different kind of information I'm using only strings here. To use the program use as arguments in the console "w" to create new file followed by the file name and the strings you want save to the file (as objects). Example: "+w TestFile.obj Thats Just A Test+". Then to read it use "r" (for reading), followed by the file name. Example "+r TestFile.obj+". As a result you'll see that all the strings that are saved in the file can be successfully read back. Then do the same: "+w TestFile.obj Thats Second Test+" and then read again "+r TestFile.obj+". What will happen is that the strings only from the first sessions will be read and the ones from the second session will not.
    I am sorry for making this that long but I couldn't explain it more simple. If someone can give me a solution I'll be happy to hear it! ^.^ I'll also be glad if someone propose different approach of the problem! Here is the code:
    import java.io.*;
    class Fio
         public static void main(String[] args)
              try
                   if (args[0].equals("w"))
                        FileOutputStream fos = new FileOutputStream(args[1], true);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);
                        for (int i = 2; i < args.length ; i++)
                             oos.writeObject(args);
                        fos.close();
                   else if (args[0].equals("r"))
                        FileInputStream fis = new FileInputStream(args[1]);
                        ObjectInputStream ois = new ObjectInputStream(fis);
                        for (int i = 0; i < fis.available(); i++)
                             System.out.println((String)ois.readObject());
                        fis.close();
                   else
                        System.out.println("Wrong args!");
              catch (IndexOutOfBoundsException exc)
                   System.out.println("You must use \"w\" or \"r\" followed by the file name as args!");
              catch (IOException exc)
                   System.out.println("I/O exception appeard!");
              catch (ClassNotFoundException exc)
                   System.out.println("Can not find the needed class");

    How to read appended objects from file with ObjectInputStream? The short answer is you can't.
    The long answer is you can if you put some work into it. The general outline would be to create a file with a format that will allow the storage of multiple streams within it. If you use a RandomAccessFile, you can create a header containing the length. If you use streams, you'll have to use a block protocol. The reason for this is that I don't think ObjectInputStream is guaranteed to read the same number of bytes ObjectOutputStream writes to it (e.g., it could skip ending padding or such).
    Next, you'll need to create an object that can return more InputStream objects, one per stream written to the file.
    Not trivial, but that's how you'd do it.

  • Appending Objects to a File using serialization

    hi,
    I was wondering if I can append objects in a single file. For example, suppose there are 2 .java files. 1st file creates the Serialized file caleed sl.dat, opens it, writes the object, close it down.
    The 2nd file nw open that same file, sl.dat, in append mode, writes one more object and close it down.
    next I want to read each object of the file. Will it work in this fashion?
    I found a quite interesting discussion here. but it says that it is not possible.. is that true?
    Any clue? Is there any other mean to achieve this?

    arin wrote:
    kajbj wrote:
    I've done it by subclassing ObjectOutputStream. I could then add a constructor that took an argument that indicated if a header should be written or not.Can you explain what I need to done here... I have a class which extends ObjectOutputStream but what to do after that? What do u mean by header in the constructor?? Please help meYou do know that you have the source code for the class if you have a JDK? The source for ObjectOutputStream is in the src.zip.
    The normal constructor calls writeStreamHeader, and that is the problem if you are going to append data since you don't want to get the stream header more than once. Create a constructor that takes a boolean as argument, and only calls writeStreamHeader if the boolean is true.
    Kaj

  • Why cant i append objects to a file

    i'm amazed that i cannot append objects to a file
    as i did earlier in c++
    it gives StreamCorrupt error

    Ashutosh.options4u wrote:
    i'm amazed that i cannot append objects to a file
    as i did earlier in c++Whatever you did in C++ didn't involve ObjectOutputStream or Java Serialization. You can append anything but objects to a file in Java. The reasons are two:
    (a) ObjectOutputStream writes a header which ObjectInputStream expects to find at the beginning of the stream and nowhere else
    (b) closing an ObjectOutputStream and starting a new one to append to the same file breaks the specified semantics for preservation of object graphs under Serialization, which can only be implemented within a single instance of ObjectOutputStream.
    it gives StreamCorrupt errorI agree.

  • Appending objects in text file and searching.......

    I have been trieng to implement simple search operation on the class objects stored in the text file. but when i try to append new objects in the same file and search for the same; java.io.StreamCorruptedException is thrown. wat the problem is, that wen i append to the text file; it stores some header information before the actual object is stored and on the deserialization, this header information is causing the exception. the same header information is stored every time, i append to the file. anybody knws hw to get past it??? my code is as given below:
    package coding;
    import java.io.BufferedReader;
    import java.io.EOFException;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.PrintWriter;
    import java.io.Serializable;
         class Employee implements Serializable{
              private static final long serialVersionUID = 1L;
              String name;
              int id;
              public int getId() {
                   return id;
              public void setId(int id) {
                   this.id = id;
              public String getName() {
                   return name;
              public void setName(String name) {
                   this.name = name;
              public Employee(String name, int id) {
                   this.name = name;
                   this.id = id;
         public class FileSearch{
         public static void main(String[] args) throws IOException
              /*Entering the records into the file*/
              Employee ob = null;
              File file=new File("c:\\abc.txt");
              InputStreamReader isr=new InputStreamReader(System.in);
              BufferedReader stdin=new BufferedReader(isr);
              char fileExist='y';
              if(file.exists())
                   System.out.println("File already exists!!!");
                   System.out.println("Append New Records(press y) Or Search Existing File(press any other button)?:");
                   String strTemp=stdin.readLine();
                   fileExist=strTemp.charAt(0);
              else
                   System.out.println("File doesnt exist; creating new file......");
              if(fileExist=='y')
                   FileOutputStream fos=new FileOutputStream(file,true);
                   ObjectOutputStream oos=new ObjectOutputStream(fos);
                   char choice='y';
                   System.out.println("Enter records:");
                   while(choice=='y')
                        System.out.println("enter id:");
                        String id_s=stdin.readLine();
                        int id=Integer.parseInt(id_s);
                        System.out.println("enter name:");
                        String name=stdin.readLine();
                        ob=new Employee(name,id);
                        try
                             oos.writeObject(ob);
                             //count++;
                             oos.flush();
                        catch(Exception e)
                             e.printStackTrace();
                        System.out.println("Enter more records?(y/n)");
                        String str1=stdin.readLine();
                        choice=str1.charAt(0);
                   oos.close();
              /*Searching for the record*/
              System.out.println("Enter Record id to be searched:");
              String idSearchStr=stdin.readLine();
              int idSearch=Integer.parseInt(idSearchStr);
              try
                   FileInputStream fis=new FileInputStream(
                             file);
                   ObjectInputStream ois=new ObjectInputStream(fis);
                   int flag=1;
                   FileReader fr=new FileReader(file);
                   int c=fr.read();
                   for(int i=0;i<c;i++)
                        Object ff=ois.readObject();
                        Employee filesearch=(Employee)ff;
                        if(filesearch.id==idSearch)
                             flag=0;
                             break;
                   ois.close();
                   if(flag==1)
                        System.out.println("Search Unsuccessful");
                   else
                        System.out.println("Search Successful");
              catch(Exception e)
                   e.printStackTrace();
    }

    966676 wrote:
    All what I need to elect by who this word repeated. But I don't know really how to make It, maybe LinkedListYou should choose the simplest type fullfilling your needs. In this case I'd go for <tt>HashSet</tt> or <tt>ArrayList</tt>.
    or I dont know, someone could help me?You need to introduce a variable to store the actual name which must be resetted if an empty line is found and then gets assigned the verry next word in the file.
    bye
    TPD

  • Appending Objects in an internal Table

    Hallo Guru,
    I'm looking for a way to append newly created objects of a class "XYZ" in an internal table.
    Best Regards,
    Kais

    Hi
    U need to append the object in the table like a normal workarea:
    CLASS LC_MY_CLASS DEFINITION.
      PUBLIC SECTION.
        METHODS: CONSTRUCTOR
                  IMPORTING P_NAME TYPE CHAR20,
                 WRITE_NAME.
      PRIVATE SECTION.
        DATA: NAME TYPE CHAR20.
    ENDCLASS.                    "LC_MY_CLASS DEFINITION
    CLASS LC_MY_CLASS IMPLEMENTATION.
      METHOD CONSTRUCTOR.
        NAME = P_NAME.
      ENDMETHOD.                    "CONSTRUCTOR
      METHOD WRITE_NAME.
        WRITE: / NAME.
      ENDMETHOD.                    "WRITE_NAME
    ENDCLASS.                    "LC_MY_CLASS IMPLEMENTATION
    TYPES: TY_OBJ TYPE REF TO LC_MY_CLASS.
    DATA: MY_OBJ TYPE REF TO LC_MY_CLASS,
          T_OBJ  TYPE TABLE OF TY_OBJ.
    START-OF-SELECTION.
      CREATE OBJECT MY_OBJ
        EXPORTING
          P_NAME = 'MAX'.
      APPEND MY_OBJ TO T_OBJ.
      CREATE OBJECT MY_OBJ
        EXPORTING
          P_NAME = 'MAX 2'.
      APPEND MY_OBJ TO T_OBJ.
      LOOP AT T_OBJ INTO MY_OBJ.
        CALL METHOD MY_OBJ->WRITE_NAME.
      ENDLOOP.
    Max

  • Duplicated objects  makes error in stream apply

    Hi Experts,
    We have a oracle10G R4 stream in 32 bit window.
    I got lots of message as ora1732 not leagl operation on view.
    After checking, I found there are duplicated object name for table /materialized view and some inclouse view.
    I got news in case of a Materialized Views, the base table name and materialized view can have same object name
    in the same schema.
    Do you encounter this issue? When I set a neg rule for object name to avoid view for capture. it also lost capture for table with same object name.
    How do we fix this issue?
    Thanks
    JIM

    Is oracle10G R4 available in market. ;-)

  • Having trouble with ObjectInput/ObjectOutput Streams

    how do you get a program to both listen for incoming objects on the ObjectInputStream, and be able to write objects to the ObjectOutputStream on the same socket?
    Right now part of my server looks like this:
    public void run(){
    try {
    objIn = new ObjectInputStream(connection.getInputStream());
    objOut = new ObjectOutputStream(connection.getOutputStream());
    objOut.writeObject(courier);
    while(true){
    courier = (Info)objIn.readObject();
    filterCmd(courier);
    } catch (Exception e) {
    }finally{
    }and part of my client looks like this:
    public void run(){
    try {
    OutputStream os = connection.getOutputStream();
    objOut = new ObjectOutputStream(os);
    objOut.flush();
    InputStream is = connection.getInputStream();
    objIn = new ObjectInputStream(is);
    try{
    while (true){
    Info courierIn = (Info) objIn.readObject();
    filterCmd(courierIn);
    }catch (Exception e){
    }They connect correctly and when the "courier" object gets written by the server before the server enters the "listening loop" everything works fine: it comes out on the other side, gets filtered and dislpayed correctly (not shown). The problem occurs when I invoke this method to write another object back to the server from the client:
    public void send(String cmd){
    try {
    courierOut.cmd = cmd;
    objOut.writeObject(courierOut);
    } catch (IOException ex) {
    Logger.getLogger(PlayerProfile.class.getName()).log(Level.SEVERE, null, ex);
    }(it's the same class of object, but a different instance). When I invoke this method from a swing button, I get this:
    java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.net.SocketInputStream.read(SocketInputStream.java:182)
    at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2249)
    at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:2542)
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2552)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1297)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at c2.PlayerProfile.run(PlayerProfile.java:687)
    at java.lang.Thread.run(Thread.java:619)This is how I set up a similar program except using Scanner and PrintWriter.
    I have read the docs on the reset method and reset exception, but it doesn't makes sense in this situation to me. How is this sort of thing (have a server and client write objects back and forth) usually done?
    Thanks so much.
    Matt

    paul.miner wrote:
    I think ObjectInputStream and ObjectOutputStream will close the underlying stream when they are closed.Correct. So will any other stream. And that will close the socket. And that will close the input stream if you closed the output stream, or vice versa.
    Furthermore, some streams that come in pairs like this will not always read all the bytes written (e.g. GZIPInputStream does not always read all the bytes written by GZIPOutputStream). I don't know if this is true of these two streams, but just something to keep in mind.If you read an ObjectInputStream until you get an EOFException, you have read all the data.
    Regarding the first problem, you could wrap your connection's streams with a filter that simply drops and request to close() it.Regarding the first problem, don't close the stream! and don't let it be GC'd either. You can't keep using new streams on the same socket, for lots of reasons, so they always have to be instance variables of the same class that has the Socket itself as an instance variable.
    Regarding the second (possible) problemIt doesn't exist.

  • Can I append object's attribute by Jaxb

    Dear all,
    I need to add various of object's attributes constantly. I mean that I need to use JAxb to app xml content then write it to xml file , and the repeat this step again and again.Can I use Jaxb to do so ? If so, how can I do that. Thanks.

    Hi Joseph,
    You can use the Class as like BO in the workflow.
    Please check the below links to know more about Classes in the workflow.
    /people/jocelyn.dart/blog/2006/06/28/getting-started-with-abap-oo-for-workflow-using-the-ifworkflow-interface
    /people/jocelyn.dart/blog/2006/07/25/using-abap-oo-methods-in-workflow-tasks
    http://wiki.sdn.sap.com/wiki/display/ABAP/UsingABAPOOmethodsinWorkflowTasks
    Thanks,
    Viji.

  • Error appending objects to file

    Here is the source code i'm using
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    public class SerialiseTest {
         public static void main(String[] args) {
              TestObject o1 = new TestObject(1);
              try {
                   ObjectOutputStream oout = new ObjectOutputStream(
                             new FileOutputStream("output.dat",true));
                   oout.writeObject(o1);
                   oout.flush();
                   oout.close();
                   ObjectInputStream oin = new ObjectInputStream(
                             new FileInputStream("output.dat"));
    while(true){
                   Object ob1 = oin.readObject();
                   System.out.println(ob1);
              } catch (Exception exc){
                   exc.printStackTrace();
    class TestObject implements Serializable {
         private int foo;
         public TestObject(int i) {
              foo = i;
         public String toString() {
              return "TestObject::"+foo;
    here is the exception
    java.io.StreamCorruptedException: invalid type code: AC
                        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
                        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
                        at SerialiseTest.main(SerialiseTest.java:26)

    Do something else. You didn't say what your requirements were or why you were trying to do that, so you can't expect much of an answer.

  • Object Replacement with the existing one int the Serialization Stream..

    when u readObject() from serialization its fine,but if u change any of its fields or value of variable and than store it back,it append new object to the Serialization Stream.
    I want to store that object back to its original position in the stream.
    please help me in this regard..

    you need to use (override) the readResolve() and writeReplace() methods in the classes that you'll be switching their instances/values/etc. with something else.
    Look at the javadocs for the java.io.Serializable interface.
    Another option is to subclass the ObjectInputStream and override the resolveObject method. (see java.io.ObjectInputStream). You'll probably also have to subclass ObjectOutputStream and override the writeObjectOverride method.
    You really only need to use one of these mechanisms, not both.
    - Andrew

  • Blocking reading object from stream

    I have a thread that continually reads objects from the input stream, so I use the readObject() method. My problem is that it doesn't block when there is no input. it just throws exceptions and I cant find a way to check first if there's anything output from the other side of the stream so do a check. thank you!

    Sorry! My mistake! I was confused!

  • Stream Corruption  - Servley can read object?

    I get the below exception when I increase the size of an objcet that I serialise. I have an object - it contains a vector. If this vector has 10000 rows then my sevlet reads it out of a database fine (blob format inside MySQL). If I add another vector of any size to the object then i get stream corrupted. If I try to add any class attributes the again stream corrupted.
    Using JDK1.4 and Tomcat3.3.2
    java.io.StreamCorruptedException
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1291)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1593)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1261)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1830)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1636)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1264)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1593)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1261)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1830)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1636)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1264)
    at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1593)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1261)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1830)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1636)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1264)
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1830)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1756)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1636)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1264)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:322)
    at cbo.ShowSession.getStatement(ShowSession.java:97)
    at cbo.ShowSession.doGet(ShowSession.java:45)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
    at org.apache.tomcat.core.Handler.service(Handler.java:287)
    at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
    at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
    at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
    at org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection(Ajp12ConnectionHandler.java:166)
    at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
    at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
    at java.lang.Thread.run(Thread.java:536)
    Thanks for any help !

    thanks already have done the flushing I think the error is something to dowith 64kb limits in the UTF encoding scheme. Objects headers that are greater than 64kb seem to mess up!
    Have some dukes for sensible suggestions though

Maybe you are looking for

  • Having trouble setting up headset with microphone

    Hi, I own a satellite A135 that is currently running on Windows Vista Home Edition. I tried using a headset with mic but somehow the mic always turns to mute after I've exited the set-up page. I'm starting to get frustrated, particularly since I need

  • I want to set one parameter

    I have a menu, who send some parameter to a report like code of the company. When the report recevied those parameter (Code of the company), in User Parameters (Report ). I try modify one of the "User Parameter " like P_Planta when I select this and

  • Update statement using function

    Environment: Win7 and SQL server 2008 R2 Tools: SQL management tool 2008 R2 Problem: I have been trying to update id numbers in the staging table. T-SQL statement updates all id number in the staging table, but what if the we have multiple records fo

  • Not able to import components into IR

    Dear all, I just installed ecc 6.0 in my home PC. installationall went perfect and its working fine. PROBLEM: I created a software Component in SLD. but when i got to Integration Repository and try to Import from SLD in Tools  its comming as " Unable

  • Lexical paramaters

    Am very new to Oracl Reports - and SQL. I have the Oracle U training manuals, and Pinnacle's book, have used the online help. I'm sorry, but I do not find any simple explanation on exactly how to use the lexical parameters. I've created 4 user parame