Results 1 to 4 of 4
- 02-28-2011, 12:19 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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?
- 02-28-2011, 02:25 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-28-2011, 04:49 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
hey it worked.. thanks JosAH... u saved my time and brain :)
- 02-28-2011, 05:09 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Singleton serialization / deserialization
By DerekRaimann in forum New To JavaReplies: 4Last Post: 02-28-2011, 01:38 AM -
Enum singleton serialization and deserialization
By bassfero in forum Advanced JavaReplies: 16Last Post: 09-08-2010, 01:37 PM -
Serialization/Deserialization Error
By andrepezzo in forum Advanced JavaReplies: 2Last Post: 12-16-2008, 05:36 PM -
how to deserialize multiple objects in a file
By xcallmejudasx in forum Advanced JavaReplies: 11Last Post: 12-16-2008, 05:29 PM -
Serialization/Deserialization Error
By andrepezzo in forum NetworkingReplies: 0Last Post: 12-16-2008, 04:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks