测试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;
}

猜你喜欢

转载自www.cnblogs.com/yxfyg/p/12407566.html