一、对象序列化:
1.在做对象数据传输时,由于对象中的数据很庞大。在传输之前,需要将对象打散成字节序列,以利于传输。这个过程叫序列化过程。

2.达到目的地后,又需要将字节序列还原成对象,这叫反序列化过程。

二、对象流使用示例:

package com;  

import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.ObjectInputStream;  
import java.io.ObjectOutputStream;  
import java.io.OutputStream;  

public class ObjectStreamTest {  
    /** 
     * 写入对象 
     *  
     * @param obj 
     *            要写入文件的对象 
     */  
    public void writerObj(Object obj) {  
        OutputStream out = null;// 写入流  
        ObjectOutputStream objOut = null;// 对象写入流  
        try {  
            out = new FileOutputStream("2.txt");  
            objOut = new ObjectOutputStream(out);  
            objOut.writeObject(obj); // 写入对象  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                // 关闭流  
                objOut.close();  
                out.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

    /** 
     * 读取文件,返回文件中写入的对象 
     *  
     * @return 
     * @throws 
     */  
    public Student read() {  
        InputStream in = null;// 读取流  
        ObjectInputStream objIn = null;// 对象读取流  
        try {  
            in = new FileInputStream("2.txt");  
            objIn = new ObjectInputStream(in);  
            // 读取对象  
            Object obj = objIn.readObject();// 读取对象  
            return (Student) obj;  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                // 关闭流  
                objIn.close();  
                in.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  

        }  
        return null;  
    }  

    public static void main(String[] args) {  
        ObjectStreamTest t = new ObjectStreamTest();  
        t.writerObj(new Student(1, "张三", "男"));  
        Student s = t.read();  
        System.out.println(s.getId() + "  " + s.getName() + "  " + s.getSex());  
    }  

}
package com;  

import java.io.Serializable;  

public class Student implements Serializable {  
    private int id;  
    private String name;  
    private transient String sex;// transient修饰属性的关键字,对象序列化时,该属性的值不作传输  

    public Student() {  
        super();  
    }  

    public Student(int id, String name, String sex) {  
        super();  
        this.id = id;  
        this.name = name;  
        this.sex = sex;  
    }  

    public int getId() {  
        return id;  
    }  

    public void setId(int id) {  
        this.id = id;  
    }  

    public String getName() {  
        return name;  
    }  

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

    public String getSex() {  
        return sex;  
    }  

    public void setSex(String sex) {  
        this.sex = sex;  
    }  

}

转载自:关于序列化和对象流

results matching ""

    No results matching ""