序列化和反序列化

来源:互联网 发布:南风知我意1txt百度云 编辑:程序博客网 时间:2024/06/06 05:36
    一:
            序列化和反序列化的用法,
objectoutputstream 和objectinputstream 
,要在父类后面加上
//public class Person implements Serializable 
public class Object {

    public static void main(String[] args) {

        Person p=new Person("1001",23,"tom");

        File file=new File("E:"+File.separator+"note.txt");

        ObjectOutputStream oos=null;

        ObjectInputStream ois=null;
        try {


            //构建输出流对象

        oos=new ObjectOutputStream

                (new FileOutputStream(file));

        oos.writeObject(p);

        oos.flush();

        oos.close();
       //构建输入流对象
        ois=new ObjectInputStream(new FileInputStream(file));

        Person p2=(Person) ois.readObject();  

System.out.println(p2.getId()+"\t"+p2.getAge()+"\t"+p2.getName());

        } catch (Exception e) {

            e.printStackTrace();

        }finally{

            if(ois!=null){

                try {

                    ois.close();

                } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();
  

0 0
原创粉丝点击