android将对象序列化到文件:直接写文件与用Serializable接口的对比

1.用文件读写1024个对象的日志

10-09 16:12:44.493 6385-6385/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 230 ms
10-09 16:12:44.546 6385-6385/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 52 ms

10-09 16:31:23.964 24295-24295/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 241 ms
10-09 16:31:24.011 24295-24295/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 47 ms


10-09 16:31:48.225 24888-24888/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 225 ms
10-09 16:31:48.272 24888-24888/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 46 ms


10-09 16:32:44.947 26161-26161/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 230 ms
10-09 16:32:44.995 26161-26161/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 47 ms

2.用Serializable序列化接口1024个对象的日志

10-09 16:17:34.203 9878-9878/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 504 ms
10-09 16:17:34.295 9878-9878/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 90 ms


10-09 16:24:15.843 18643-18643/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 523 ms
10-09 16:24:15.943 18643-18643/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 99 ms


10-09 16:25:34.634 20412-20412/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 560 ms
10-09 16:25:34.727 20412-20412/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 91 ms


10-09 16:30:52.559 23712-23712/com.example.tt.downtest D/Serializable_TAG: write 1024 apples need 513 ms
10-09 16:30:52.654 23712-23712/com.example.tt.downtest D/Serializable_TAG: read 1024 apples need 94 ms 

3.结论

  1w,10w,100w,不测试了,直接写文件.

4.示例

  1 package com.example.tt.downtest;
  2 
  3 
  4 import android.util.Log;
  5 
  6 import java.io.File;
  7 import java.io.FileInputStream;
  8 import java.io.FileNotFoundException;
  9 import java.io.FileOutputStream;
 10 import java.io.IOException;
 11 import java.io.ObjectInputStream;
 12 import java.io.ObjectOutputStream;
 13 import java.io.RandomAccessFile;
 14 import java.io.Serializable;
 15 import java.util.ArrayList;
 16 
 17 public class Apple implements Serializable{
 18 
 19     static final String TAG = "Serializable_TAG";
 20     static final long serialVersionUID = 10000000000000000L;
 21 
 22     public int      mID;
 23     public String mName;
 24     public String   mClass;
 25 
 26     @Override
 27     public String toString() {
 28         return "Apple{" +
 29                 "mID = " + mID +
 30                 ", mName = '" + mName + '\'' +
 31                 ", mClass = '" + mClass + '\'' +
 32                 '}';
 33     }
 34 
 35 /*   
 36     //自定义的简单加密,把id加密了。
 37    private void readObject(ObjectInputStream in) {
 38        try {
 39            //readFields是个键值对,也可以直接用in.readInt()等.写的时候用out.writeFields();才能与in.readFields()匹配,
 40            ObjectInputStream.GetField readFields = in.readFields();
 41            mID = readFields.get("mID", -1) - 11;  //自定义的简单加密,把id加密了。
 42            mName = (String) readFields.get("mName","default-mName");
 43 
 44 
 45        } catch (IOException e) {
 46            e.printStackTrace();
 47        } catch (ClassNotFoundException e) {
 48            e.printStackTrace();
 49        }
 50    }
 51    private void writeObject(ObjectOutputStream out){
 52        try {
 53            ObjectOutputStream.PutField fields = out.putFields();
 54            fields.put("mID",mID + 11);                 //自定义的简单加密,把id加密了。
 55            fields.put("mName",mName );
 56            out.writeFields();
 57        } catch (IOException e) {
 58            e.printStackTrace();
 59        }
 60    }*/
 61 
 62     private void readObject(ObjectInputStream in) {
 63         try {
 64             mID = in.readInt();
 65             mName = in.readUTF();
 66             mClass = in.readUTF();
 67         } catch (IOException e) {
 68             Log.d(TAG, "IOException " + e.getMessage());
 69         }
 70     }
 71     private void writeObject(ObjectOutputStream out){
 72         try {
 73             out.writeInt(mID);
 74             out.writeUTF(mName);
 75             out.writeUTF(mClass);
 76 
 77         } catch (IOException e) {
 78             Log.d(TAG, "IOException " + e.getMessage());
 79         }
 80     }
 81     static void testNObject(File path){
 82         int len = 1024;
 83         long begin,end;
 84         ArrayList<Apple> writeApples = new ArrayList<>(len);
 85         String file = path + "/apple.n";
 86 
 87         for (int i = 0 ; i < len;++i){
 88             Apple apple = new Apple();
 89             apple.mID = i;
 90             apple.mName = "name" + i;
 91             apple.mClass = "class" + i ;
 92             writeApples.add(apple);
 93         }
 94         try {
 95             //write
 96             FileOutputStream fos = new FileOutputStream(file);
 97             ObjectOutputStream oos = new ObjectOutputStream(fos);
 98             begin = System.currentTimeMillis();
 99             for (int i = 0; i < len;++i){
100                 Apple app = writeApples.get(i);
101                 oos.writeObject(app);
102             }
103             end = System.currentTimeMillis();
104             Log.d(TAG, "write " +  len + " apples need " + (end - begin) + " ms");
105             oos.close();
106             fos.close();
107 
108             //read
109             ArrayList<Apple> readApples = new ArrayList<>(len);
110             FileInputStream fis = new FileInputStream(file);
111             ObjectInputStream ois = new ObjectInputStream(fis);
112 
113             begin = System.currentTimeMillis();
114             for (int i = 0; i < len; ++i) {
115                 Apple apple = (Apple) ois.readObject();
116                 readApples.add(apple);
117             }
118             end = System.currentTimeMillis();
119             Log.d(TAG, "read " +  len + " apples need " + (end - begin) + " ms");
120             ois.close();
121             fis.close();
122 
123             for (int i = 0 ; i < len; ++i){
124                 Apple apple = readApples.get(i);
125                 Log.d(TAG, apple.toString());
126             }
127 
128         } catch (FileNotFoundException e) {
129             Log.d(TAG, "FileNotFoundException " + e.getMessage());
130         } catch (IOException e) {
131             Log.d(TAG, "IOException " + e.getMessage());
132         } catch (ClassNotFoundException e) {
133             Log.d(TAG, "ClassNotFoundException " + e.getMessage());
134         }
135     }
136 
137     static void testNFiles(File path){
138         int len = 1024;
139         long begin,end;
140 
141         String file = path + "/apple.list";
142         ArrayList<Apple> apples = new ArrayList<>(len);
143         for (int i = 0 ; i < len; ++i){
144             Apple app = new Apple();
145             app.mID = i;
146             app.mName = "name" + i;
147             app.mClass = "class" + i;
148             apples.add(app);
149         }
150         try {
151             //write
152             RandomAccessFile writer = new RandomAccessFile(file,"rw");
153 
154             begin = System.currentTimeMillis();
155             writer.writeInt(len);
156             for (int i = 0;i < len; ++i){
157                 Apple app = apples.get(i);
158                 writer.writeInt(app.mID);
159                 writer.writeUTF(app.mName);
160                 writer.writeUTF(app.mClass);
161             }
162             writer.close();
163             end = System.currentTimeMillis();
164             Log.d(TAG, "write " +  len + " apples need " + (end - begin) + " ms");
165 
166             //read
167             RandomAccessFile reader = new RandomAccessFile(file,"rw");
168             len = reader.readInt();
169             ArrayList<Apple> readApples = new ArrayList<>(len);
170 
171             begin = System.currentTimeMillis();
172             for (int i = 0; i < len; ++i) {
173                 Apple app = new Apple();
174                 app.mID = reader.readInt();
175                 app.mName = reader.readUTF();
176                 app.mClass = reader.readUTF();
177                 readApples.add(app);
178             }
179             reader.close();
180             end = System.currentTimeMillis();
181             Log.d(TAG, "read " +  len + " apples need " + (end - begin) + " ms");
182 
183             for (int i = 0; i < len; ++i){
184                 Apple app = readApples.get(i);
185                 Log.d(TAG, app.toString());
186             }
187 
188         } catch (FileNotFoundException e) {
189             e.printStackTrace();
190         } catch (IOException e) {
191             e.printStackTrace();
192         }
193     }
194 
195     public static void testSerializable(File path){
196 //        testNObject(path);
197         testNFiles(path);
198     }
199 }

 

转载于:https://www.cnblogs.com/sjjg/p/5909295.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值