對象流指的是可以直接把一個對象
以流的形式
傳輸給其他的介質,比如硬盤
一個對象以流的形式進行傳輸,叫做序列化
。 該對象所對應的類,必須是實現Serializable接口
public class Main {
public static void main(String[] args) {
Zoom z1 = new Zoom();
z1.name = "kitty";
h.age = 5;
//準備一個文件用於保存該對象
File f =new File("f:/testfile/test.zoom");
try(
//創建對象輸出流
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos =new ObjectOutputStream(fos);
//創建對象輸入流
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois =new ObjectInputStream(fis);
) {
oos.writeObject(h);
Zoom z2 = (Zoom) ois.readObject();
System.out.println(z2.name);
System.out.println(z2.age);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}