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

          日歷

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          QQ的消息服務

          搜索

          •  

          積分與排名

          • 積分 - 29066
          • 排名 - 1421

          最新評論


          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;
           }
          }

          主站蜘蛛池模板: 汽车| 永春县| 扶绥县| 南通市| 涡阳县| 吐鲁番市| 沙洋县| 偏关县| 龙里县| 奈曼旗| 磐安县| 项城市| 哈巴河县| 抚宁县| 治县。| 莱州市| 宜章县| 通许县| 洪湖市| 定安县| 龙里县| 潼南县| 黑水县| 淅川县| 苏尼特右旗| 海阳市| 黄骅市| 铜川市| 昆山市| 滦南县| 大英县| 淳安县| 九寨沟县| 龙游县| 周至县| 万荣县| 德化县| 灵川县| 叶城县| 顺平县| 河津市|