用java實現一個簡單的序列化的例子(轉)

ba發表於2007-08-15
用java實現一個簡單的序列化的例子(轉)[@more@]package test;
import java.io.*;
public class Test implements Serializable{
int i=0;
//不讓變數j序列化
transient int j=0;
public static void main(String[] args) {

Test test=new Test();
test.i=3;
test.j=7;
System.out.println(test.i);
System.out.println(test.j);
//存
FileOutputStream fileStream = null;
try {
fileStream = new FileOutputStream("c: est.obj");
ObjectOutputStream out = new ObjectOutputStream(fileStream);
out.writeObject(test);
out.close();
}
catch (Exception ex) {
}
try {
//取
FileInputStream fileStream1 = new FileInputStream("c:product.obj");
ObjectInputStream in = new ObjectInputStream(fileStream1);
Test test1 = (Test)in.readObject();
System.out.println(test1.i);
//j沒有被序列化
System.out.println(test1.j);
in.close();
}
catch (Exception ex) {
}
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617731/viewspace-958243/,如需轉載,請註明出處,否則將追究法律責任。

相關文章