The job is done in the serializable classes
I have made an example here, as simple as possible. You should be able
to see the trick here.
Good luck
Code:
/**
* Simple serialization example
*/
import java.io.*;
import java.util.*;
/**
* Class for storing car owner info
*/
class CarOwner implements Serializable {
private String name=null;
private String address=null;
// constructor
public CarOwner(String name, String address) {
this.name=name;
this.address=address;
}
public String toString() {
return "Owner: " + name + ", Address: " + address;
}
// write the class
private void writeObject(ObjectOutputStream out) throws IOException {
Serilalizing.serializeStringOut(name,out);
Serilalizing.serializeStringOut(address,out);
}
// read the class, in same sequence as it was written...
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
name=Serilalizing.serializeStringIn(in);
address=Serilalizing.serializeStringIn(in);
}
}
/**
* Class for storing basic car information
*/
class CarInfo implements Serializable {
private String make = null;
private String model = null;
private int year = -1;
private CarOwner owner=null;
// constructor
public CarInfo(String make, String model, int year, CarOwner owner) {
this.make = make;
this.model = model;
this.year = year;
this.owner=owner;
}
public String toString() {
return "Make: " + make + ", Model: " + model + ", Year: " + year + " "+owner;
}
// write the class
private void writeObject(ObjectOutputStream out) throws IOException {
Serilalizing.serializeStringOut(make,out);
Serilalizing.serializeStringOut(model,out);
out.writeInt(year);
out.writeObject(owner);
}
// read the class, in same sequence as it was written...
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
make=Serilalizing.serializeStringIn(in);
model=Serilalizing.serializeStringIn(in);
year=in.readInt();
owner=(CarOwner)in.readObject();
}
}
/**
* Car Driver class ...hehe
*/
public class Serilalizing {
public Serilalizing() {}
public void makeAndStoreCars (String outPath) throws IOException {
ArrayList list=new ArrayList();
FileOutputStream ostream = new FileOutputStream(outPath);
ObjectOutputStream out = new ObjectOutputStream(ostream);
// make some owners and cars and store in ArrayList
CarOwner owner1=new CarOwner("John Smith","Penny Lane 123");
CarOwner owner2=new CarOwner("Donald Duck","QuackStreet 313");
CarInfo car1=new CarInfo("Ford","Focus",2001,owner1);
list.add(car1);
CarInfo car2=new CarInfo("Volvo","V70",2004,owner2);
list.add(car2);
out.writeObject(list);
out.close(); // <---- important
System.out.println("Stored: "+list.size()+" cars");
}
public void readCarInfo (String inPath)
throws IOException, ClassNotFoundException {
FileInputStream istream = new FileInputStream(inPath);
ObjectInputStream in = new ObjectInputStream(istream);
ArrayList list=(ArrayList)in.readObject();
System.out.println("Read: "+list.size()+" cars");
for (int i=0;i<list.size();i++) {
CarInfo car=(CarInfo)list.get(i);
System.out.println(car);
}
in.close(); // <--- important
}
/**
* This is for reading/writing a string as a series of bytes,
* I have not found any other way to do this. I have declared the two io
* methods here as static so they can be used 'all over the place'. I could
* have declared these as non-static methods in both Car and CarOwner, but
* I am too lazy.
*/
public static void serializeStringOut(String s, ObjectOutputStream out)
throws IOException {
out.writeInt(s.length());
out.write(s.getBytes());
}
public static String serializeStringIn(ObjectInputStream in)
throws IOException {
int len=in.readInt();
byte [] b=new byte[len];
in.read(b);
return new String(b);
}
// ********************* MAIN ********************
public static void main(String[] args) {
Serilalizing se = new Serilalizing();
try {
se.makeAndStoreCars("c:\\tmp\\acarlist.ser");
se.readCarInfo("c:\\tmp\\acarlist.ser");
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
}