package test2;标签:对象,printStackTrace,专用,Demon03,oos,catch,new,null,ois From: https://www.cnblogs.com/Leizi-go/p/17356859.html
import java.io.*;
//对象专用流
public class Demo04 {
public static void main(String[] args) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("H:\\Java2234\\Test.txt");
oos = new ObjectOutputStream(fos);
Student lucy = new Student("Lucy", 15);
oos.writeObject(lucy);
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("H:\\Java2234\\Test.txt");
ois = new ObjectInputStream(fis);
Student stu = (Student) ois.readObject();
System.out.println(stu);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}