zoukankan      html  css  js  c++  java
  • 序列化与反序列化

    public class TestObject {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    //对象的序列化--将对象以二进制的形式输出(没有确定输出到哪儿去)
    // ArrayList<Student> al = new ArrayList<Student>();
    // Student stu0 = new Student("HJ", 25,"四川","成都","九眼桥第三桥洞");
    // Student stu1 = new Student("YP", 27,"四川","成都","九眼桥第2桥洞");
    // al.add(stu0);
    // al.add(stu1);
    //
    // ObjectOutputStream oos = null;
    //
    // try {
    // oos = new ObjectOutputStream(new FileOutputStream("student.data"));
    // oos.writeObject(al);
    // } catch (FileNotFoundException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }finally{
    // if(oos != null){
    // try {
    // oos.close();
    // } catch (IOException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // }
    // }


    //反序列化--将输入的二进制流转换为对象(同样不确定二进制流的数据源)
    ArrayList<Student> al = null;
    ObjectInputStream ois = null;

    try {
    //使用了装饰器模式
    ois = new ObjectInputStream(new FileInputStream("student.data"));
    al = (ArrayList<Student>)ois.readObject();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally{
    if(ois != null){
    try {
    ois.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    System.out.println(al.get(0));
    System.out.println(al.get(1));
    }

    }

    感觉好绕  头痛

  • 相关阅读:
    spring
    redis
    CentOS 6 安装教程(转载)
    Ajax&JSON
    课时2:RequestMapping映射及个属性
    课时1:SpringMVC环境搭建及第一个程序
    课时2:Spring整合MyBatis的几种方式 前面一个课时已经讲了一种思路了
    课时1:Spring整合MyBatis思路
    课时14:Bean的生命周期,通过import引入分散的spring文件
    课时13:作用域
  • 原文地址:https://www.cnblogs.com/whj1986556646/p/5598089.html
Copyright © 2011-2022 走看看