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. cocos2dx 制作单机麻将(五)

    cocos2dx 制作单机麻将(五) 麻将逻辑6 最基础的4人麻将逻辑(轮流循环出牌, 之前学的都能用上  跑起来了!!!) 最基础的麻将逻辑 依据自己须要 设置麻将人数GAME_PLAYER 基本流 ...

  2. Python常用的几种常用的内置函数

    abs(x)              用于返回绝对值 divmod(x,y)       函数中传入两个数字,返回的是x/y的一个结果的元组(商,余数) pow(x,y)            用于 ...

  3. 积跬步,聚小流------Bootstrap学习记录(3)

    响应式作为Bootstrap的一大特色.栅格系统可谓是功不可没,既然如此,那我们就来看一下栅格系统是怎样帮助bootstrap实现响应式布局的呢? 1.什么是栅格系统 我们能够从Bootstrap的官 ...

  4. wsdl2objc定制(一)namespace

    1.问题抛出: 如今还是有非常多人使用 wsdl2objc 来调用webservice,可是有时候会有不开心的事情发生, <soap:Envelope xmlns:soap="http ...

  5. iOS学习之动画效果的实现

    // //  ViewController.m //  UI-动画练习 // //  Created by jzq_mac on 15/7/22. //  Copyright (c) 2015年 jz ...

  6. web编程非常实用的在线工具大全

    目前,不管是前端开发人员还是个人站长,经常需要一些代码处理类的工具,比如:代码对比.代码格式化.图标制作等.有时就是一时急用可电脑上又没有安装相关的软件,这里为大家收集了一些我们经常会用到的在线工具. ...

  7. BC1.2的一些心得

    什么叫DCD DataContact Detect(DCD) 1.首先是DCD 2.然后是Primary detection 3.然后是Secondary detection 检測充电的条件是VBUS ...

  8. css3动画入门transition、animation

    css3动画 transition.animation CSS3 transition demo <!DOCTYPE html> <html> <head> < ...

  9. hdoj-2090-算菜价(水题)

    算菜价 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  10. 正则表达式ab?c匹配的字符串是?(B)

    A:abcd B:abc C:abcbc D:aEbc