Java IO流详尽解析(十)

2014-11-24 08:14:35 · 作者: · 浏览: 7
意:Serializable接口实现的操作其实是吧一个对象中的全部属性进行序列化,当然也可以使用我们上使用是Externalizable接口以实现部分属性的序列化,但是这样的操作比较麻烦,

当我们使用Serializable接口实现序列化操作的时候,如果一个对象的某一个属性不想被序列化保存下来,那么我们可以使用transient关键字进行说明:

【案例 】使用transient关键字定制序列化和反序列化操作

package IO;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
/**
 * 序列化和反序列化的操作
 * */
public class serDemo{
    public static voidmain(String[] args) throws Exception{
        ser(); // 序列化
        dser(); // 反序列话
    }
 
    public static void ser()throws Exception{
        File file = newFile("d:" + File.separator + "hello.txt");
        ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream(
                file));
        out.writeObject(newPerson1("rollen", 20));
        out.close();
    }
 
    public static void dser()throws Exception{
        File file = newFile("d:" + File.separator + "hello.txt");
        ObjectInputStreaminput = new ObjectInputStream(new FileInputStream(
                file));
        Object obj =input.readObject();
        input.close();
       System.out.println(obj);
    }
}
 
class Person1 implements Serializable{
    public Person1(){
 
    }
 
    public Person1(Stringname, int age){
        this.name = name;
        this.age = age;
    }
 
    @Override
    public String toString(){
        return "姓名:" +name + "  年龄:" +age;
    }
 
    // 注意这里
    private transient Stringname;
    private int age;
}

【运行结果】:

姓名:null 年龄:20

【案例 】序列化一组对象

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
/**
 * 序列化一组对象
 * */
public class SerDemo1{
    public static voidmain(String[] args) throws Exception{
        Student[] stu = { newStudent("hello", 20), new Student("world", 30),
                newStudent("rollen", 40) };
        ser(stu);
        Object[] obj = dser();
        for(int i = 0; i 
    

     

参考文献:

1、http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html

2、http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html