Solved

serializing multiple objects in the same file

Posted on 2010-11-14
5
472 Views
Last Modified: 2012-06-21
Thanks experts for extended support..

Can some one please give a simple example of serializing multiple objects in the same file and deserializing them
0
Comment
Question by:hipal
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132761
Just call oos.writeObject several times with the corresponding ois.readObject
0
 

Author Comment

by:hipal
ID: 34132771
so what if I want to read 4th object that I wrote ?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 34132787
You need to read them in order. Or serialize a List and then you can access by index
import java.io.*;
public class WriteRead {
    public static void main(String[] args) throws Exception {
	ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("x.ser"));
	for(int i = 0;i < 5;i++) {	
	    oos.writeObject("" + i);
	}
	oos.close();
	ObjectInputStream ois = new ObjectInputStream(new FileInputStream("x.ser"));
	for(int i = 0;i < 5;i++) {	
	    System.out.println(ois.readObject());
	}
	ois.close();
    }
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 34132790
add them to a ArrayList (or array) and then serialize/deserialize the list/array
that will handle reading/writing all objects in one call
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34134989
:)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 67
Work with App store 7 51
How do I remove an object from a 3 23
Convert from a json string array to a Java object 3 28
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now