正文
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 ms10-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 ms10-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 ms10-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 ms10-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 ms10-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 ms10-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.结论
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 ms10-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 ms10-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 ms10-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
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 ms10-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 ms10-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 ms10-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);
}
}
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);
}
}