天空是蓝色的

做好软件为中国 #gcc -c helloworld.c -o helloworld.o //编译目标文件 #gcc helloworld.o -o helloworld //编译成可执行exe #helloworld //运行exe
数据加载中……
对象序列化存储读取实现代码
 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     * 写对象到指定文件中
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     * 读指定文件的对象到对象组内
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     * 显示对象内容
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     * 显示对象内容
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 阅读(442) 评论(0)  编辑  收藏 所属分类: 基础知识


只有注册用户登录后才能发表评论。


网站导航: