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接口的对比的更多相关文章

  1. 快学Scala 第十五课 (二进制读取文件,写文件,访问目录,序列化)

    二进制读取文件: val file = new File("F:\\scalaWorkspace\\ScalaLearning\\files\\test.txt") val in ...

  2. asp adodb.stream读取文件和写文件

    读取文件操作: '------------------------------------------------- '函数名称:ReadTextFile '作用:利用AdoDb.Stream对象来读 ...

  3. Python: 读文件,写文件

    读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...

  4. android bundle 对象 序列化

    Android使用Intent.putSerializable()进行数据传递,或者使用Bundle进行数据传递,实质上都是进行的Serializable数据的操作,说白了都是传递的原数据的一份拷贝, ...

  5. Python按行读取文件、写文件

    Python按行读取文件 学习了:https://www.cnblogs.com/scse11061160/p/5605190.html file = open("sample.txt&qu ...

  6. JAVA读文件和写文件的的代码模版

    有的时候经常为真么读写文件最合理发愁,因为JAVA提过读写文件的方式太多了(C更甚至,fopen & open又有多少人傻傻分不去,更别说ReadFile了). 这里个人绝对比较好的写法,仅供 ...

  7. python读文件和写文件

    f=open('D:\\wangdongjie\\files\\webservice\\baidu\\3.txt','r+') f.write('中国电视台1][][23qwe12f我是一个小小的石头 ...

  8. Java学习笔记——IO操作之对象序列化及反序列化

    对象序列化的概念 对象序列化使得一个程序可以把一个完整的对象写到一个字节流里面:其逆过程则是从一个字节流里面读出一个事先存储在里面的完整的对象,称为对象的反序列化. 将一个对象保存到永久存储设备上称为 ...

  9. Tinking in Java ---Java的NIO和对象序列化

    前面一篇博客的IO被称为经典IO,因为他们大多数都是从Java1.0开始就有了的:然后今天这篇博客是关于NIO的,所以的NIO其实就是JDK从1.4开始,Java提供的一系列改进的输入/输出处理的新功 ...

随机推荐

  1. 鸟哥的Linux私房菜-----13、账号管理

  2. 多校训练hdu --Nice boat(线段树,都是泪)

    Nice boat Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total ...

  3. sql跟踪及tkprof使用

    简述 在oracle数据库中,awr是关于数据库系统总体的负载情况和运行情况的报告.而当系统负载都显示正常,而client运行某些动作响应非常慢,或者某些终端连接的会话运行缓慢或异常时,就须要用到会话 ...

  4. Jenkins系列之-—04 配置用户和权限控制

    一.安装插件 插件名称:Role-based Authorization Strategy Role Strategy Plugin插件可以对构建的项目进行授权管理,让不同的用户管理不同的项目. 二. ...

  5. 模仿猫眼电影App一个动画效果

    看真正的猫眼效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/font/5a6L5L2T/fontsize/400/f ...

  6. Android 4.4.2 动态加入JNI库方法记录 (二 app应用层)

    欢迎转载,务必注明出处:http://blog.csdn.net/wang_shuai_ww/article/details/44458553 源代码下载地址:http://download.csdn ...

  7. [IT新应用]无线投影技术

    会议室内投影时,经常会有笔记本与投影仪之间因兼容性等无法切换的现象. 了解了下,无线投影方案的厂家大致如下: 1.http://www.taco.net.cn/ 2.巴可无线投影 https://ww ...

  8. windows 目录空格

    Window下安装Scala出现:此时不应有 \scala\bin\..\lib\jline-2.14.5.jar 原因很简单,scala默认安装到了Program Files (x86)文件夹下,目 ...

  9. 省市区三级-sql脚本:

    /*Navicat MySQL Data Transfer Source Server : moiraiSource Server Version : 50631Source Host : 192.1 ...

  10. HDU2121 Ice_cream’s world II —— 最小树形图 + 不定根 + 超级点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 Ice_cream’s world II Time Limit: 3000/1000 MS (J ...