美文网首页
java系列19:对象流

java系列19:对象流

作者: 3e1094b2ef7b | 来源:发表于2017-06-23 08:53 被阅读0次

    将对象写入到流,这个过程叫做序列化。
    反之,称为反序列化。

    如果要用于网络中传输的数据,则这些数据必须要实现序列化。

    例1:

    // Student.java
    
    package test;
    
    public class Student {
        String name;
        int age;
        String sex;
    
        public Student(String name, int age, String sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    
        public void study() {
            System.out.println("我在认真学习");
        }
    
        @Override
        public String toString() {
            return "我的名字:" + name + ",我的年龄:"
                    + age + ",我的性别:" + sex;
        }
    }
    
    // Test.java
    
    package test;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    
    public class Test {
        public static void main(String[] args) {
            Student stu = new Student("李行之", 25, "男");
            File file = new File("a.txt");
            try {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(stu);
                oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    运行结果,未进行序列化,因此无法将对象写入流中。13行:oos.writeObject(stu);

    例2:

    // 1. Student.java

    package test;
    
    import java.io.Serializable;
    
    /*
     *  implements Serializable:实现序列化接口。
     *  它没有任何方法需要实现,只是一个标识。
     *  序列化之后,会有一个编号植入到对象中;当写入到磁盘时,会将编号一起写入磁盘。
     */
    public class Student implements Serializable{
        String name;
        int age;
        String sex;
        // transient:不序列化某些属性
        transient long money = 10000000;
    
        public Student(String name, int age, String sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    
        public void study() {
            System.out.println("我在认真学习");
        }
    
        @Override
        public String toString() {
            return "我的名字:" + name + ",我的年龄:" 
                    + age + ",我的性别:" + sex + ",我的钱:" + money;
        }
    }
    
    // 2. Test.java
    
    package test;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    
    /**
     * 序列化
     * @author xiangdonglee
     *
     */
    public class Test {
        public static void main(String[] args) {
            Student stu = new Student("李行之", 25, "男");
            File file = new File("a.txt");
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(stu);
                oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    序列化结果
    // 3.Test1.java
    
    package test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    /**
     * 反序列化
     * @author xiangdonglee
     *
     */
    public class Test1 {
        public static void main(String[] args) {
            File file = new File("a.txt");
            if(file.exists()) {
                try {
                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
                    Student stu = (Student) ois.readObject();
                    System.out.println(stu);
                    ois.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    transient修饰的money属性没有被序列化

    相关文章

      网友评论

          本文标题:java系列19:对象流

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