import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class SampleSerialize implements Serializable{
/**
*
*/
private static final long serialVersionUID = 7464990594348541444L;
/**
*
*/
private int id;
private String name;
private boolean isboy;
private Date birthday;
/**
* @param id
* @param name
* @param isboy
* @param birthday
*/
public SampleSerialize(int id, String name, boolean isboy, Date birthday) {
super();
this.id = id;
this.name = name;
this.isboy = isboy;
this.birthday = birthday;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the isboy
*/
public boolean isIsboy() {
return isboy;
}
/**
* @param isboy the isboy to set
*/
public void setIsboy(boolean isboy) {
this.isboy = isboy;
}
/**
* @return the birthday
*/
public Date getBirthday() {
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
//输出
public void printInfo() {
System.out.println("id : " + this.id);
System.out.println("name : " + this.name);
System.out.println("isboy : " + this.isboy);
System.out.println("birthday : " + this.birthday);
}
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("lib/data1.txt"));
ObjectInputStream objIn = new ObjectInputStream(new FileInputStream("lib/data1.txt"));
SampleSerialize ss = new SampleSerialize(1,"Joary",true,new Date());
SampleSerialize ss1 = new SampleSerialize(2,"坤",true,new Date());
objOut.writeObject(ss);
objOut.writeObject(ss1);
SampleSerialize s = (SampleSerialize)objIn.readObject();
s.printInfo();
s = (SampleSerialize)objIn.readObject();
s.printInfo();
}
}
序列化应用
最新推荐文章于 2021-08-23 15:05:14 发布