美文网首页
IO流之对象流

IO流之对象流

作者: 冯宏志 | 来源:发表于2021-01-29 15:59 被阅读0次

    对象流的使用

    • ObjectInputStream
      ObjectOutputStream

    • 作用:用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来

    1、序列化ObjectOutputStream

    将内存中的java对象保存到磁盘中或通过网络传输出去

     @Test
        public void test1(){
            ObjectOutputStream oos = null;
            try {
                //第一步
                oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
    
                //第二步
                oos.writeObject(new String("你好!"));
                oos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(oos != null)
                    try {
                        //第三步
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    
    2、反序列化ObjectInputStream
    @Test
        public void test2(){
            //第一步
            ObjectInputStream  ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream("object.dat"));
    
                //第二步
                Object obj = ois.readObject();
                String str = (String)obj;
                System.out.println(str);//你好!
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (ois != null)
                    //第三步
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    
    3、自定义类的序列化和反序列化

    自定义类需要满足如下的要求,方可序列化
    1、需要实现接口:Serializable
    2、需要当前类提供一个全局常量:public static final long serialVersionUID
    3、除了当前类需要实现Serializable接口外,还必须保证其内部所有属性也必须是可序列化的。(默认情况下, 基本数据类型可序列化)

    补充:ObjectOutputStreamObjectInputStream不能序列化statictransient修饰的成员变量(如果不想让类中的某个成员变量序列化,加个transient

     @Test
        public void test3(){
            ObjectOutputStream oos = null;
            try {
                //第一步
                oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
    
                //第二步
                oos.writeObject(new Person("fhz",21));
                oos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(oos != null)
                    try {
                        //第三步
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    
        @Test
        public void test4(){
            //第一步
            ObjectInputStream  ois = null;
            try {
                ois = new ObjectInputStream(new FileInputStream("object.dat"));
    
                //第二步
                Object obj1 = ois.readObject();
                Person p1 = (Person)obj1;
                System.out.println(p1);//Person{name='fhz', age=21}
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (ois != null)
                    //第三步
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    

    相关文章

      网友评论

          本文标题:IO流之对象流

          本文链接:https://www.haomeiwen.com/subject/pnqpgktx.html