public class SerializableBean implements Serializable {
private String name;
private Date birthDate;
private Double money;
public void setName(String name) {
this.name = name;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public void setMoney(Double money) {
this.money = money;
}
public String toString() {
SimpleDateFormat format = new SimpleDateFormat("dd/MMMM/yyyy");
return name + " " + format.format(birthDate) + " " + money;
}
}
// data to be serialized
SerializableBean bean = new SerializableBean();
bean.setBirthDate(new Date());
bean.setMoney(10000000d);
bean.setName("Rochmat Santoso");
// create new file if one doesn't exist
File file = new File("myTextFile.txt");
// serialize data
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(bean);
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
// deserialize data
FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
SerializableBean bean1 = (SerializableBean) objectInputStream.readObject();
System.out.println(bean1);
objectInputStream.close();
fileInputStream.close();
Tuesday, October 4, 2011
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment