首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Object源 序列化

2012-08-25 
Object流 序列化import java.io.*public class TestObjectIO {?public static void main(String args[])

Object流 序列化

import java.io.*;

public class TestObjectIO {
?public static void main(String args[]) throws Exception {
??T t = new T();
??t.k = 8;
??FileOutputStream fos = new FileOutputStream("d:/java/io/testobjectio.dat");
??ObjectOutputStream?oos = new ObjectOutputStream(fos);
??oos.writeObject(t);
??oos.flush();
??oos.close();
??
??FileInputStream fis = new FileInputStream("d:/java/io/testobjectio.dat");
??ObjectInputStream?ois = new ObjectInputStream(fis);
??T tReaded = (T)ois.readObject();
??System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
??
?}
}

class T
?implements?Serializable
{
?int i = 10;
?int j = 9;
?double d = 2.3;
?transient?int k = 15;
}

?

另外externalizable接口,用于自定义序列化方式

热点排行