天空是藍(lán)色的

          做好軟件為中國(guó) #gcc -c helloworld.c -o helloworld.o //編譯目標(biāo)文件 #gcc helloworld.o -o helloworld //編譯成可執(zhí)行exe #helloworld //運(yùn)行exe
          數(shù)據(jù)加載中……
          對(duì)象序列化存儲(chǔ)讀取實(shí)現(xiàn)代碼
           1import java.io.FileInputStream;
           2import java.io.FileOutputStream;
           3import java.io.ObjectInputStream;
           4import java.io.ObjectOutputStream;
           5import java.io.Serializable;
           6
           7public class serialStore implements Serializable {
           8
           9    public serialStore() {
          10    }
          // serialStore()
          11
          12    /**
          13     * 寫對(duì)象到指定文件中
          14     */

          15    public void writeFile(String strFile, Object[] o) {
          16        try {
          17            FileOutputStream fos = new FileOutputStream(strFile);
          18            ObjectOutputStream oos = new ObjectOutputStream(fos);
          19            for (int i = 0; i < o.length; i++{
          20                oos.writeObject(o[i]);
          21            }

          22            oos.flush();
          23        }
           catch (Exception e) {
          24            e.printStackTrace();
          25        }

          26    }
          // writeFile()
          27
          28    /**
          29     * 讀指定文件的對(duì)象到對(duì)象組內(nèi)
          30     */

          31    public Object[] readFile(String strFile, int objectNum) {
          32        Object o[] = new Object[objectNum];
          33        try {
          34            FileInputStream fis = new FileInputStream(strFile);
          35            ObjectInputStream ois = new ObjectInputStream(fis);
          36            for (int i = 0; i < o.length; i++{
          37                o[i] = ois.readObject();
          38            }

          39        }
           catch (Exception e) {
          40            e.printStackTrace();
          41        }

          42        return o;
          43    }
          // readFile()
          44
          45    /**
          46     * 顯示對(duì)象內(nèi)容
          47     */

          48    public void log(Object[] o) {
          49        for (int i = 0; i < o.length; i++{
          50            System.out.println(o[i]);
          51        }

          52    }
          // log()
          53
          54    /**
          55     * 顯示對(duì)象內(nèi)容
          56     */

          57    public void log(Object o) {
          58        System.out.println(o);
          59    }
          // log()
          60
          61    public static void main(String arg[]) {
          62        Object o[] = new Object[2];
          63        o[0= new String("objectName");
          64        o[1= new Integer(25);
          65        serialStore ss = new serialStore();
          66        String strFile = "serial.abc";
          67        ss.writeFile(strFile, o);
          68        ss.log(ss.readFile(strFile, o.length));
          69    }
          //main() 
          70
          71}

          72/** serialStore */
          73
          74

          posted on 2005-11-09 10:03 bluesky 閱讀(456) 評(píng)論(0)  編輯  收藏 所屬分類: 基礎(chǔ)知識(shí)

          主站蜘蛛池模板: 澎湖县| 黑龙江省| 邻水| 临沂市| 多伦县| 望都县| 尤溪县| 汶川县| 莎车县| 茌平县| 湘潭市| 兴宁市| 新津县| 三亚市| 绥江县| 九寨沟县| 图木舒克市| 临沭县| 上杭县| 绥化市| 苏尼特左旗| 宜阳县| 珲春市| 温州市| 行唐县| 府谷县| 通城县| 洞头县| 井研县| 泉州市| 广元市| 邢台县| 台山市| 西盟| 海门市| 清丰县| 习水县| 道孚县| 曲水县| 武功县| 定结县|