TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
package com.zhangjie.stream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class TestObjectIO {
public static void main(String[] args) {
T t = new T();
t.k = 8;
FileOutputStream fos;
try {
fos = new FileOutputStream("E:\\java\\IO\\output.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(t);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
FileInputStream fis;
try {
fis = new FileInputStream("E:\\java\\IO\\output.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
T tRead = (T)ois.readObject();
System.out.println(tRead.i+" "+tRead.j+" "+tRead.d+" "+tRead.k);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
class T implements Serializable{
int i = 10;
int j = 9;
double d = 2.3;
//带有transient的参数不会被序列化
transient int k = 15;
} |
|