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. HTC 328T 如何恢复出厂设置

    设置-存储-恢复出厂设置(在存储的最下面,往下拉)

  2. 微信小程序之 SideBar(侧栏分类)

    项目目录: 模拟数据: utils / data.js function getSData() { var data = [ { "id": 1, "tree" ...

  3. 获取Windows用户所有的账户名

    /// <summary> /// 设置用户密码 /// </summary> [DllImport("Netapi32.dll")] extern sta ...

  4. 如何将Python的py程序打包成跨平台的exe文件

    在编写了自己的第一个可以爬写网页源代码的程序之后,发现如果在没有安装了pythonLDLE程序的计算机上根本就跑不出来.所以开始寻找可以将程序打包成跨平台运行的exe文件. 经过自己费力的谷歌没有一个 ...

  5. IE9版本号下面ajax 跨域问题解决

    ajax跨域请求数据在谷歌火狐我本地IE11都是没问题的. 让測试就发现问题了,IE8下请求不到数据.然后我查看一下自己写的js看有没有不兼容问题.但是都没有啊.为什么就请求不到呢. 我把ajax的e ...

  6. js调试记录,将客户的调试信息保存到服务器端的一个小方法。

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. PyTorch 高级实战教程:基于 BI-LSTM CRF 实现命名实体识别和中文分词

    前言:译者实测 PyTorch 代码非常简洁易懂,只需要将中文分词的数据集预处理成作者提到的格式,即可很快的就迁移了这个代码到中文分词中,相关的代码后续将会分享. 具体的数据格式,这种方式并不适合处理 ...

  8. CAS和ABA

    1 CAS compare and swap的缩写,详见乐观锁和悲观锁. 2 ABA 就是说,我获取的旧值是A,然后被人修改成了B,但是又被人修改成了A,我就认为并没有修改,更新内存. 解决办法,给每 ...

  9. high-level operations on files and collections of files

    11.10. shutil — High-level file operations — Python 3.6.5 documentation https://docs.python.org/3/li ...

  10. Java之jdk命令行工具详解

    JPS---虚拟机进程状况工具 常用的参数: -l 输出Java应用程序的main class的完整包 -q 仅显示pid,不显示其它任何相关信息 -m 输出传递给main方法的参数 -v 输出传递给 ...