测试Object流(Serializable接口和transient关键字)

import java.io.*;
import java.util.*;

public class ObjectSerializable{
    
    public static void main(String[] args){
        try{
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/Java/ObjectSerializable/test.txt"));
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Java/ObjectSerializable/test.txt"));
            oos.writeObject(new T());
            oos.flush();
            oos.close();
            T t = (T)ois.readObject();
            System.out.println("a=" + t.a + " b=" + t.b + " c=" + t.c + " d=" + t.d);
            ois.close();
        } catch(IOException e){
                e.printStackTrace();
        }  catch(ClassNotFoundException e){
                e.printStackTrace();
        } 
    }
}

class T implements Serializable{
    int a = 2;
    double b = Math.random();
    char c = 'a';
    transient int d = 10;
}

 

上一篇:java中用byte[]数组实现的队列和用Byte[]实现的队列实际占用空间对比


下一篇:循环结构