posts - 3, comments - 15, trackbacks - 0, articles - 26
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理


          import java.io.*;

          public class SerializationDemo {
           public static void main(String args[]) {

            //Object serialization
            try {
             MyClass object1 = new MyClass("Hello", -7, 2.7e10);
             System.out.println("object1:" + object1);
             FileOutputStream fos = new FileOutputStream("serial");
             ObjectOutputStream oos = new ObjectOutputStream(fos);
             oos.writeObject(object1);
             oos.flush();
             oos.close();
            } catch (Exception e) {
             System.out.println("Exception during serialization:" + e);
             System.exit(0);
            }

            //Object deserialization
            try {
             MyClass object2;
             FileInputStream fis = new FileInputStream("serial");
             ObjectInputStream ois = new ObjectInputStream(fis);
             object2 = (MyClass) ois.readObject();
             ois.close();
             System.out.println("object2:" + object2);
            } catch (Exception e) {
             System.out.println("Exception during deserialization:" + e);
             System.exit(0);
            }
           }
          }

          class MyClass implements Serializable {
           String s;

           int i;

           double d;

           public MyClass(String s, int i, double d) {
            this.s = s;
            this.i = i;
            this.d = d;
           }

           public String toString() {
            return "s=" + s + ";i=" + i + ";d=" + d;
           }
          }

          主站蜘蛛池模板: 元谋县| 马龙县| 交城县| 平利县| 洪湖市| 资溪县| 佳木斯市| 秦皇岛市| 松江区| 深水埗区| 桑植县| 安阳县| 怀宁县| 乳山市| 明星| 苏尼特左旗| 遂溪县| 吴堡县| 东乌珠穆沁旗| 南澳县| 天长市| 遵义市| 新干县| 新野县| 寻甸| 普洱| 景泰县| 大城县| 石景山区| 汉沽区| 麻栗坡县| 开远市| 武胜县| 兖州市| 黄山市| 连平县| 潼关县| 会昌县| 宝兴县| 平山县| 长武县|