首页 > 编程语言 > 详细

java ObjectOutputStream

时间:2016-04-19 13:43:51      阅读:159      评论:0      收藏:0      [点我收藏+]
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Employee {
    private String name;

    Employee(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

class SerEmployee extends Employee implements Serializable {
    SerEmployee(String name) {
        super(name);
    }
}

public class SerializationDemo {
    public static void main(String[] args) {
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("employee.dat"));
            SerEmployee se = new SerEmployee("John Doe");
            System.out.println(se);
            oos.writeObject(se);
            oos.close();
            oos = null;
            System.out.println("se object written to file");
            ois = new ObjectInputStream(new FileInputStream("employee.dat"));
            se = (SerEmployee) ois.readObject();
            System.out.println("se object read from file");
            System.out.println(se);
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }

        catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (oos != null)
                try {
                    oos.close();
                } catch (IOException ioe) {
                    assert false; // shouldn‘t happen in this context
                }
            if (ois != null)
                try {
                    ois.close();
                } catch (IOException ioe) {
                    assert false; // shouldn‘t happen in this context
                }
        }
    }
}

 

java ObjectOutputStream

原文:http://www.cnblogs.com/rojas/p/5407548.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!