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

    序列化流与反序列化流

    用于从流中读取对象的

    操作流 ObjectInputStream    称为 反序列化流

    用于向流中写入对象的操作流 ObjectOutputStream   称为 序列化流

     特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。

    1 对象序列化流ObjectOutputStream

    注意:只能将支持 java.io.Serializable 接口的对象写入流中

    复制代码
        public static void method01() throws IOException{
            //序列化:实体类必须实现Serializable接口
            Person p=new Person("zhangsan",18);
            //明确目的地
            FileOutputStream fos=new FileOutputStream("E:\java\person.txt");
            //创建序列化流
            ObjectOutputStream oos=new ObjectOutputStream(fos);
            //向文件中写入对象
            oos.writeObject(p);
            //释放资源
            oos.close();
        }
    复制代码

    2 对象反序列化流ObjectInputStream

     

    复制代码
        //反序列化
        public static void method02() throws IOException, ClassNotFoundException{
            //明确数据源
            FileInputStream fis=new FileInputStream("E:\java\person.txt");
            //创建反序列化流
            ObjectInputStream ois=new ObjectInputStream(fis);
            Object obj=ois.readObject();
            Person p=(Person)obj;
            System.out.println(p);
            //释放资源
            ois.close();
        }
    复制代码
  • 相关阅读:
    UVA 12545 Bits Equalizer
    UVA 1610 Party Games
    UVA 1149 Bin Packing
    UVA 1607 Gates
    UVA 12627 Erratic Expansion
    UVA10562-Undraw the Trees(递归)
    UVA10129-Play on Words(欧拉路径)
    UVA816-Abbott's Revenge(搜索进阶)
    UVA1103-Ancient Messages(脑洞+dfs)
    UVA839-Not so Mobile
  • 原文地址:https://www.cnblogs.com/lxzwhite/p/10683399.html
Copyright © 2011-2022 走看看