java序列化反序列化ArrayList集合

    /**
     * 生成序列化
     * 
     * @throws IOException
     */
    public void serialization() throws IOException {
        File File = new File("D:\\File\\ut");
        OutputStream os = new FileOutputStream(File);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(list);
        oos.close();
        os.close();
    }

    /**
     * 反序列化
     * 
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void Inserialization() throws IOException, ClassNotFoundException {
        File File = new File("D:\\File\\ut");
        InputStream is = new FileInputStream(File);
        ObjectInputStream ois = new ObjectInputStream(is);
        List<Students> tList = (List<Students>) ois.readObject();
        System.out.println(tList.size());
        for (Students s : tList) {
            System.out.println(s.getNum() + s.getName() + s.getAge()
                    + s.getGenber() + T.format(s.getTime())
                    + T.format(s.getXtime()));
        }
        ois.close();
        is.close();
    }

Students类必须implements Serializable,才能序列化;

package Administrate;

import java.io.Serializable;
import java.util.Date;

import org.ietf.jgss.Oid;

public class Students implements Serializable{
    private int Num;//学生编号
    private String Name;//学生姓名
    private Byte Age;//学生年龄
    private String Genber;//学生性别
    private Date Time;//创建时间
    private Date Xtime; //修改时间
    public Students(int Num,String Name,Byte Age,String Genber,Date Time,Date Xtime){
        this.Num=Num;
        this.Name=Name;
        this.Age=Age;
        this.Genber=Genber;
        this.Time=Time;
        this.Xtime=Xtime;
    }
    public Students(String Name,Byte Age,String Genber){
        this.Name=Name;
        this.Age=Age;
        this.Genber=Genber;
    }
    public Students(){

    }
    /**
     * 封装
     */
    public int getNum() {
        return Num;
    }
    public void setNum(int num) {
        Num = num;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public Byte getAge() {
        return Age;
    }
    public void setAge(Byte age) {
        Age = age;
    }
    public String getGenber() {
        return Genber;
    }
    public void setGenber(String genber) {
        Genber = genber;
    }
    public Date getTime() {
        return Time;
    }
    public void setTime(Date time) {
        Time = time;
    }
    public Date getXtime() {
        return Xtime;
    }
    public void setXtime(Date xtime) {
        Xtime = xtime;
    }
    public void xiugai(){
        this.Xtime=new Date();
    }
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值