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

          日歷

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          QQ的消息服務

          搜索

          •  

          積分與排名

          • 積分 - 29004
          • 排名 - 1422

          最新評論


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

          主站蜘蛛池模板: 大庆市| 衡南县| 乌拉特前旗| 伊通| 巴彦县| 昌图县| 阳山县| 裕民县| 亳州市| 永顺县| 临西县| 资阳市| 密云县| 平遥县| 上饶市| 永春县| 平塘县| 荔波县| 清水县| 固镇县| 方山县| 望城县| 固始县| 庐江县| 宁强县| 青浦区| 连南| 昌都县| 遂宁市| 淮滨县| 大名县| 南部县| 七台河市| 稷山县| 凤翔县| 抚顺市| 泸西县| 尚志市| 郑州市| 蒙自县| 武安市|