Results 1 to 4 of 4
  1. #1
    hanx is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Serialization and Deserialization of multiple objects from single file

    hey guys, i wrote this java program which is kind of contact directory containing "name" and "id" of persons. what i want to do is store or save multiple contact or object of different value to a single file and retrieve back to the program.. but my luck , i am unable to acheive it.. the output is very different... here is the code of my program :

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class PersonControl
    {
    	public static void main(String args[]) throws Exception
    	{
    		Scanner kb = new Scanner(System.in);// this will be used later 
    		PData data = null;
    		ObjectInputStream in = null;
    		ObjectOutputStream out = null;
    		
    		out = new ObjectOutputStream(new  FileOutputStream("PData.dat",true));
    		data = new PData();
    		data.set(101,"Jimmy");
    		data.show();
    		out.writeObject(data);
    		//out.flush();
    		data.set(102,"Tony");
    		data.show();
    		out.writeObject(data);
    		//out.flush();
    		data.set(103,"cyrus");
    		data.show();
    		out.writeObject(data);
    		//out.flush();
    		out.close();
    		
    		in = new ObjectInputStream(new FileInputStream("PData.dat"));
    		PData temp;
    		temp = (PData) in.readObject();
    		temp.show();
    		temp = (PData) in.readObject();
    		temp.show();
    		temp = (PData) in.readObject();
    		temp.show();
    		in.close();
    		
    	}
    	
    }// end of class
    
    
    
    class PData implements Serializable
    {
    	private String name;
    	private int id;
    	
    	void set(int id_temp, String t_name)
    	{
    		this.id = id_temp;
    		this.name = t_name;
    	}
    	
    	void show()
    	{
    		System.out.println("ID NO = " + id);
    		System.out.println("Name = " + name);
    	}
    }

    The output of the above program is :
    C:\jj\object\Personal>java PersonControl
    ID NO = 101
    Name = Jimmy
    ID NO = 102
    Name = Tony
    ID NO = 103
    Name = cyrus
    ID NO = 101
    Name = Jimmy
    ID NO = 101
    Name = Jimmy
    ID NO = 101
    Name = Jimmy
    _____________________

    You see the first data is only saved and retreived from file but not rest of data.. i want to make like kind of storing multiple information or contact to a single file.. Advice?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    After writing a object to the ObjectOutputStream reset() the stream. Those ObjectOutputStreams think they´re clever by remembering which objects have been written already and they won´t write them again. The reset() method make them forget what they've been writing before.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    hanx is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    hey it worked.. thanks JosAH... u saved my time and brain :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hanx View Post
    hey it worked.. thanks JosAH... u saved my time and brain :)
    You're welcome of course and remember: all the details are in the API documentation; never guess or assume anything but always read that stuff.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Singleton serialization / deserialization
    By DerekRaimann in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 01:38 AM
  2. Enum singleton serialization and deserialization
    By bassfero in forum Advanced Java
    Replies: 16
    Last Post: 09-08-2010, 01:37 PM
  3. Serialization/Deserialization Error
    By andrepezzo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2008, 05:36 PM
  4. how to deserialize multiple objects in a file
    By xcallmejudasx in forum Advanced Java
    Replies: 11
    Last Post: 12-16-2008, 05:29 PM
  5. Serialization/Deserialization Error
    By andrepezzo in forum Networking
    Replies: 0
    Last Post: 12-16-2008, 04:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •