在程序運行過程中對象是被保存在內存中的,也就是只在程序運行過程中有效,一旦程序結束或者中止所有這個程序中的對象就都消失了。
如果需要把對象狀態保存起來,當然,有數據庫,但是初學階段,還沒學到JDBC章節,而且有時用文件記錄會更合適,現在正研究的就是文件處理,所以,現在就是要把對象狀態以文件的形式保存在磁盤中,以供將來調用。這個過程書上叫“對象串行化”(那個字讀xing還是hang我還不知道呢)。
JAVA中要實現串行化,就是讓需要串行化的類實現java.io.Serializable接口。Serializable接口沒有任何方法和屬性的聲明,空接口。書上說實現這個接口實際是做一種聲明,表明該類遵循了串行化的潛規則(沒太懂這句話)。
java.io包里專門為保存對象提供了對象操作流:ObjectInputStream和ObjectOutputStream。
java.io.ObectInputStream繼承自java.io.InputStream,同時又實現了接口ObjectInput和ObjectStreamConstants。ObjectOutputStream與之類似。
構造函數如下:
ObjectInputStream(InputStream in)
ObjectOutputStream(OutputStream out)
ObjectInputStream(InputStream in)
ObjectOutputStream(OutputStream out)
ObjectInputStream實現從輸入流中讀取一個對象用readObject()方法,ObjectOutputStream與之對應的是writeObject()方法。
舉例:
package myjavatests;
import java.io.*;
class MM implements Serializable {
int height;
double weight;
String name;
String sw;
public MM(int height, double weight, String name,String sw) {
this.height = height;
this.weight = weight;
this.name = name;
this.sw = sw;
}
}
public class SerializableTest {
public static void main(String[] args) {
MM m = new MM(176,46,"Lily","80-30-60");
System.out.println("將要保存的對象的狀態:");
System.out.println("身高:"+m.height);
System.out.println("體重:"+m.weight);
System.out.println("姓名:"+m.name);
System.out.println("三圍:"+m.sw);
try {
FileOutputStream fos = new FileOutputStream("e:\\javafiletest\\mm.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(m);
oos.close();
} catch(IOException ioe) {
System.out.println(ioe);
}
//讀取對象
m = null;
try {
FileInputStream fis = new FileInputStream("e:\\javafiletest\\mm.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
m = (MM)(ois.readObject());
ois.close();
System.out.println("恢復對象的狀態:");
System.out.println("身高:"+m.height);
System.out.println("體重:"+m.weight);
System.out.println("姓名:"+m.name);
System.out.println("三圍:"+m.sw);
} catch(IOException ioe) {
System.out.println(ioe);
} catch(ClassNotFoundException c) {
System.out.println(c);
}
}
}
import java.io.*;
class MM implements Serializable {
int height;
double weight;
String name;
String sw;
public MM(int height, double weight, String name,String sw) {
this.height = height;
this.weight = weight;
this.name = name;
this.sw = sw;
}
}
public class SerializableTest {
public static void main(String[] args) {
MM m = new MM(176,46,"Lily","80-30-60");
System.out.println("將要保存的對象的狀態:");
System.out.println("身高:"+m.height);
System.out.println("體重:"+m.weight);
System.out.println("姓名:"+m.name);
System.out.println("三圍:"+m.sw);
try {
FileOutputStream fos = new FileOutputStream("e:\\javafiletest\\mm.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(m);
oos.close();
} catch(IOException ioe) {
System.out.println(ioe);
}
//讀取對象
m = null;
try {
FileInputStream fis = new FileInputStream("e:\\javafiletest\\mm.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
m = (MM)(ois.readObject());
ois.close();
System.out.println("恢復對象的狀態:");
System.out.println("身高:"+m.height);
System.out.println("體重:"+m.weight);
System.out.println("姓名:"+m.name);
System.out.println("三圍:"+m.sw);
} catch(IOException ioe) {
System.out.println(ioe);
} catch(ClassNotFoundException c) {
System.out.println(c);
}
}
}
在NetBeans中按Shift+F6運行這個文件,得到結果:
將要保存的對象的狀態:
身高:176
體重:46.0
姓名:Lily
三圍:80-30-60
恢復對象的狀態:
身高:176
體重:46.0
姓名:Lily
三圍:80-30-60
身高:176
體重:46.0
姓名:Lily
三圍:80-30-60
恢復對象的狀態:
身高:176
體重:46.0
姓名:Lily
三圍:80-30-60
同時,在程序指定的磁盤目錄下會產生指定名稱的文件,這里是e:\javafiletest\mm.dat,試著用記事本打開,看到如下內容:
sr myjavatests.MM糱?鯠? I heightD weightL namet Ljava/lang/String;L swq ~ xp 癅G t Lilyt 80-30-60
本次記錄到此結束。
posted @ 2010-01-29 10:46 強稻 閱讀(233) | 評論 (0) | 編輯 收藏