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.示例
package com.example.tt.downtest; import android.util.Log; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.ArrayList; public class Apple implements Serializable{ static final String TAG = "Serializable_TAG";
static final long serialVersionUID = 10000000000000000L; public int mID;
public String mName;
public String mClass; @Override
public String toString() {
return "Apple{" +
"mID = " + mID +
", mName = '" + mName + '\'' +
", mClass = '" + mClass + '\'' +
'}';
} /*
//自定义的简单加密,把id加密了。
private void readObject(ObjectInputStream in) {
try {
//readFields是个键值对,也可以直接用in.readInt()等.写的时候用out.writeFields();才能与in.readFields()匹配,
ObjectInputStream.GetField readFields = in.readFields();
mID = readFields.get("mID", -1) - 11; //自定义的简单加密,把id加密了。
mName = (String) readFields.get("mName","default-mName"); } catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void writeObject(ObjectOutputStream out){
try {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("mID",mID + 11); //自定义的简单加密,把id加密了。
fields.put("mName",mName );
out.writeFields();
} catch (IOException e) {
e.printStackTrace();
}
}*/ private void readObject(ObjectInputStream in) {
try {
mID = in.readInt();
mName = in.readUTF();
mClass = in.readUTF();
} catch (IOException e) {
Log.d(TAG, "IOException " + e.getMessage());
}
}
private void writeObject(ObjectOutputStream out){
try {
out.writeInt(mID);
out.writeUTF(mName);
out.writeUTF(mClass); } catch (IOException e) {
Log.d(TAG, "IOException " + e.getMessage());
}
}
static void testNObject(File path){
int len = ;
long begin,end;
ArrayList<Apple> writeApples = new ArrayList<>(len);
String file = path + "/apple.n"; for (int i = ; i < len;++i){
Apple apple = new Apple();
apple.mID = i;
apple.mName = "name" + i;
apple.mClass = "class" + i ;
writeApples.add(apple);
}
try {
//write
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
begin = System.currentTimeMillis();
for (int i = ; i < len;++i){
Apple app = writeApples.get(i);
oos.writeObject(app);
}
end = System.currentTimeMillis();
Log.d(TAG, "write " + len + " apples need " + (end - begin) + " ms");
oos.close();
fos.close(); //read
ArrayList<Apple> readApples = new ArrayList<>(len);
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis); begin = System.currentTimeMillis();
for (int i = ; i < len; ++i) {
Apple apple = (Apple) ois.readObject();
readApples.add(apple);
}
end = System.currentTimeMillis();
Log.d(TAG, "read " + len + " apples need " + (end - begin) + " ms");
ois.close();
fis.close(); for (int i = ; i < len; ++i){
Apple apple = readApples.get(i);
Log.d(TAG, apple.toString());
} } catch (FileNotFoundException e) {
Log.d(TAG, "FileNotFoundException " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "IOException " + e.getMessage());
} catch (ClassNotFoundException e) {
Log.d(TAG, "ClassNotFoundException " + e.getMessage());
}
} static void testNFiles(File path){
int len = ;
long begin,end; String file = path + "/apple.list";
ArrayList<Apple> apples = new ArrayList<>(len);
for (int i = ; i < len; ++i){
Apple app = new Apple();
app.mID = i;
app.mName = "name" + i;
app.mClass = "class" + i;
apples.add(app);
}
try {
//write
RandomAccessFile writer = new RandomAccessFile(file,"rw"); begin = System.currentTimeMillis();
writer.writeInt(len);
for (int i = ;i < len; ++i){
Apple app = apples.get(i);
writer.writeInt(app.mID);
writer.writeUTF(app.mName);
writer.writeUTF(app.mClass);
}
writer.close();
end = System.currentTimeMillis();
Log.d(TAG, "write " + len + " apples need " + (end - begin) + " ms"); //read
RandomAccessFile reader = new RandomAccessFile(file,"rw");
len = reader.readInt();
ArrayList<Apple> readApples = new ArrayList<>(len); begin = System.currentTimeMillis();
for (int i = ; i < len; ++i) {
Apple app = new Apple();
app.mID = reader.readInt();
app.mName = reader.readUTF();
app.mClass = reader.readUTF();
readApples.add(app);
}
reader.close();
end = System.currentTimeMillis();
Log.d(TAG, "read " + len + " apples need " + (end - begin) + " ms"); for (int i = ; i < len; ++i){
Apple app = readApples.get(i);
Log.d(TAG, app.toString());
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static void testSerializable(File path){
// testNObject(path);
testNFiles(path);
}
}
android将对象序列化到文件:直接写文件与用Serializable接口的对比的更多相关文章
- 快学Scala 第十五课 (二进制读取文件,写文件,访问目录,序列化)
二进制读取文件: val file = new File("F:\\scalaWorkspace\\ScalaLearning\\files\\test.txt") val in ...
- asp adodb.stream读取文件和写文件
读取文件操作: '------------------------------------------------- '函数名称:ReadTextFile '作用:利用AdoDb.Stream对象来读 ...
- Python: 读文件,写文件
读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...
- android bundle 对象 序列化
Android使用Intent.putSerializable()进行数据传递,或者使用Bundle进行数据传递,实质上都是进行的Serializable数据的操作,说白了都是传递的原数据的一份拷贝, ...
- Python按行读取文件、写文件
Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...
- JAVA读文件和写文件的的代码模版
有的时候经常为真么读写文件最合理发愁,因为JAVA提过读写文件的方式太多了(C更甚至,fopen & open又有多少人傻傻分不去,更别说ReadFile了). 这里个人绝对比较好的写法,仅供 ...
- python读文件和写文件
f=open('D:\\wangdongjie\\files\\webservice\\baidu\\3.txt','r+') f.write('中国电视台1][][23qwe12f我是一个小小的石头 ...
- Java学习笔记——IO操作之对象序列化及反序列化
对象序列化的概念 对象序列化使得一个程序可以把一个完整的对象写到一个字节流里面:其逆过程则是从一个字节流里面读出一个事先存储在里面的完整的对象,称为对象的反序列化. 将一个对象保存到永久存储设备上称为 ...
- Tinking in Java ---Java的NIO和对象序列化
前面一篇博客的IO被称为经典IO,因为他们大多数都是从Java1.0开始就有了的:然后今天这篇博客是关于NIO的,所以的NIO其实就是JDK从1.4开始,Java提供的一系列改进的输入/输出处理的新功 ...
随机推荐
- 【APUE】线程与信号
每个线程都有自己的信号屏蔽字,但是信号的处理是进程中所有线程共享的.进程中的信号是递送到单个线程的. 线程中pthread_sigmask函数类似与进程的sigprocmask函数,可以用来阻塞信号. ...
- uva 10069 Distinct Subsequences 【dp+大数】
题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s .求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的 ...
- Visual Studio VS如何拷贝一个项目的窗体文件到另一个项目
1 比如下我有一个项目,我要把这个Config整个窗体和代码拷贝到另一个项目 2 在新项目中添加现有项,然后把这个窗体相关的三个文件都添加到新的项目中 3 然后在新窗体中就什么都有了 ...
- SQL 约束(Constraints)
SQL 约束(Constraints) SQL 约束(Constraints) SQL 约束用于规定表中的数据规则. 如果存在违反约束的数据行为,行为会被约束终止. 约束可以在创建表时规定(通过 CR ...
- vue 自定义报警组件
1.自定义报警组件 Alarm.vue <!-- 报警 组件 --> <template> <div class="alarm"> <!- ...
- PADs 元器件PCB建库
直接看图就好了,上图! 有几点需要记住的: 如果没有datasheet的情况下,与焊盘相比,阻焊大0.1mm,钢网小0.1mm.或者阻焊大0.05mm,钢网等大,具体要看引脚的间距. 焊盘太大,比如1 ...
- pip 安装速度慢解决办法
https://blog.csdn.net/liujingclan/article/details/50176597 https://blog.csdn.net/rytyy/article/detai ...
- IO流(字节流复制)01
package ioDemo; import java.io.*; /** * IO流(字节流复制) * Created by lcj on 2017/11/2. */ public class bu ...
- Chart.js docs
原文链接:http://www.bootcss.com/p/chart.js/docs/ 引入Chart.js文件 首先我们需要在页面中引入Chart.js文件.此工具库在全局命名空间中定义了Char ...
- ABAP ALV F4帮助
ALV F4帮助, 选值保存到ALV. TYPE-POOLS:slis. CLASS lcl_event_receiver DEFINITION DEFERRED. DATA: gt_fcat TYP ...