对象的序列化

对象流只有字节流,没有字符流

 

 

对象流:ObjectInputStream & ObjectOutputStream
1、对象的序列化:将内存中的对象永久的以二进制形式保存到磁盘中
2、对象的反序列化:将磁盘中保存的对象读取 

package ObjectStream;

import org.testng.annotations.Test;

import java.io.*;

/*
对象流:ObjectInputStream & ObjectOutputStream
1、对象的序列化:将内存中的对象永久的以二进制形式保存到磁盘中
2、对象的反序列化:将磁盘中保存的对象读取
 */
public class ObjectStream {
    //序列化
    @Test
    public void test1(){
        String str="abcde";
        int num=100;
        boolean b=true;
        ObjectOutputStream oos= null;
        try {
            FileOutputStream fos=new FileOutputStream("./data.dat");
            oos = new ObjectOutputStream(fos);
            oos.writeUTF(str);
            oos.writeInt(num);
            oos.writeBoolean(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //反序列化
    @Test
    public void test2(){
        ObjectInputStream ois= null;
        try {
            //1、创建FileInputStream的实例,同时打开指定文件
            FileInputStream fis=new FileInputStream("./data.dat");
            //2、创建BufferedInputStream的实例,包装对应节点流,用于提高效率
            BufferedInputStream bis=new BufferedInputStream(fis);
            //3、创建ObjectInputStream 的实例,包装对应的缓冲流,用于完成反序列化
            ois = new ObjectInputStream(bis);
            //4、读取指定文件的内容(按什么顺序序列化的就按什么顺序反序列化)
            String str = ois.readUTF();
            int num = ois.readInt();
            boolean b = ois.readBoolean();
            System.out.println(str+num+b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5、关闭流
           if (ois!=null){
               try {
                   ois.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }
}

 

 

package ObjectStream;

import org.testng.annotations.Test;

import java.io.*;

/*
对象流:ObjectInputStream & ObjectOutputStream
1、对象的序列化:将内存中的对象永久的以二进制形式保存到磁盘中
2、对象的反序列化:将磁盘中保存的对象读取
 */
public class ObjectStream {
   

    //对象的序列化
    @Test
    public void test3(){
        ObjectOutputStream oos= null;
        try {
            Person p1=new Person("张三",18);
            Person p2=new Person("李四",23);
            Person p3=new Person("王五",33);
            FileOutputStream fis=new FileOutputStream("./persons.dat");
            BufferedOutputStream bos=new BufferedOutputStream(fis);
            oos = new ObjectOutputStream(bos);
            oos.writeObject(p1);
            oos.writeObject(p2);
            oos.writeObject(p3);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package ObjectStream;

public class Person {
        private String name;
        private Integer age;

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

实现Serializable接口的类才能被序列化

package ObjectStream;

import java.io.Serializable;

public class Person implements Serializable {
        private String name;
        private Integer age;

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

package ObjectStream;

import org.testng.annotations.Test;

import java.io.*;

/*
对象流:ObjectInputStream & ObjectOutputStream
1、对象的序列化:将内存中的对象永久的以二进制形式保存到磁盘中
2、对象的反序列化:将磁盘中保存的对象读取
 */
public class ObjectStream {
    

    //对象的序列化
    @Test
    public void test3(){
        ObjectOutputStream oos= null;
        try {
            Person p1=new Person("张三",18);
            Person p2=new Person("李四",23);
            Person p3=new Person("王五",33);
            FileOutputStream fis=new FileOutputStream("./persons.dat");
            BufferedOutputStream bos=new BufferedOutputStream(fis);
            oos = new ObjectOutputStream(bos);
            oos.writeObject(p1);
            oos.writeObject(p2);
            oos.writeObject(p3);
        } 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 {
            FileInputStream fis=new FileInputStream("./persons.dat");
            BufferedInputStream bis=new BufferedInputStream(fis);
            ois = new ObjectInputStream(bis);
            Person p1 =(Person)ois.readObject();
            Person p2 =(Person)ois.readObject();
            Person p3 =(Person)ois.readObject();
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

对象序列化步骤:

①创建节点流对象
②(可选)创建缓冲流包装对应节点流,用于提高效率
③创建对象流包装对应的缓冲流,用于完成序列化
④通过writeXxx()方法完成序列化
⑤需要序列化对象的类必须实现java.io.Serializable接口

反序列化同上

属性也需要实现接口  

 string实现了接口

 

 

 

一般实现接口后提供一个序列号,方便反序列化

自动生成:

 

 

 

 

使用transient修饰的属性不会被序列化

 

static修饰的属性不会被序列化

 

 

对象流:ObjectInputStream & ObjectOutputStream
1、对象的序列化:将内存中的对象永久的以二进制形式保存到磁盘中

①创建节点流对象
②(可选)创建缓冲流包装对应节点流,用于提高效率
③创建对象流包装对应的缓冲流,用于完成序列化
④通过writeXxx()方法完成序列化
⑤需要序列化对象的类必须实现java.io.Serializable接口
⑥提供序列号private static final Long SerialVersionUID=(long型的id)L;

2、对象的反序列化:将磁盘中保存的对象读取

注意:
    ①transient修饰的属性不会被序列化
    ②static修饰的属性也不会被序列化

 

package ObjectStream;

import org.testng.annotations.Test;

import java.io.*;

/*
对象流:ObjectInputStream & ObjectOutputStream
1、对象的序列化:将内存中的对象永久的以二进制形式保存到磁盘中

①创建节点流对象
②(可选)创建缓冲流包装对应节点流,用于提高效率
③创建对象流包装对应的缓冲流,用于完成序列化
④通过writeXxx()方法完成序列化
⑤需要序列化对象的类必须实现java.io.Serializable接口
⑥提供序列号private static final Long SerialVersionUID=(long型的id)L;

2、对象的反序列化:将磁盘中保存的对象读取

注意:
    ①transient修饰的属性不会被序列化
    ②static修饰的属性也不会被序列化
 */
public class ObjectStream {
    //序列化
    @Test
    public void test1(){
        String str="abcde";
        int num=100;
        boolean b=true;
        ObjectOutputStream oos= null;
        try {
            FileOutputStream fos=new FileOutputStream("./data.dat");
            oos = new ObjectOutputStream(fos);
            oos.writeUTF(str);
            oos.writeInt(num);
            oos.writeBoolean(b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //反序列化
    @Test
    public void test2(){
        ObjectInputStream ois= null;
        try {
            //1、创建FileInputStream的实例,同时打开指定文件
            FileInputStream fis=new FileInputStream("./data.dat");
            //2、创建BufferedInputStream的实例,包装对应节点流,用于提高效率
            BufferedInputStream bis=new BufferedInputStream(fis);
            //3、创建ObjectInputStream 的实例,包装对应的缓冲流,用于完成反序列化
            ois = new ObjectInputStream(bis);
            //4、读取指定文件的内容(按什么顺序序列化的就按什么顺序反序列化)
            String str = ois.readUTF();
            int num = ois.readInt();
            boolean b = ois.readBoolean();
            System.out.println(str+num+b);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5、关闭流
           if (ois!=null){
               try {
                   ois.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }

    //对象的序列化
    @Test
    public void test3(){
        ObjectOutputStream oos= null;
        try {
            Person p1=new Person("张三",18);
            Person p2=new Person("李四",23);
            Person p3=new Person("王五",33);
            FileOutputStream fis=new FileOutputStream("./persons.dat");
            BufferedOutputStream bos=new BufferedOutputStream(fis);
            oos = new ObjectOutputStream(bos);
            oos.writeObject(p1);
            oos.writeObject(p2);
            oos.writeObject(p3);
        } 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 {
            FileInputStream fis=new FileInputStream("./persons.dat");
            BufferedInputStream bis=new BufferedInputStream(fis);
            ois = new ObjectInputStream(bis);
            Person p1 =(Person)ois.readObject();
            Person p2 =(Person)ois.readObject();
            Person p3 =(Person)ois.readObject();
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(p3);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值