序列化应用


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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值