对象专用流Demon03

发布时间 2023-04-26 17:52:40作者: 磊子记
package test2;

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();
}
}
}


}

}