当我的应用程序启动时,它会连接到数据库并下载一些 XML 格式的数据。这是布局:
<Customers>
<Customer>
<name>...</name>
...
</Customer>
<Customer>
<name>...</name>
...
</Customer>
...
</Customer>
我为每个客户创建了一个类 Customer
的对象,并将其存储在 ArrayList 中。用户可以编辑和添加每个客户的一些数据,他们可以将其上传回服务器。
问题是我不知道在本地存储数据的最佳方式是什么,所以如果我的应用程序关闭或用户不再有互联网,他们会有一个备份。我现在将所有 Customer
对象转换回 XML,然后将其存储在 SD 卡上,但我认为这不是一个好的解决方案。每个客户有大约 40 个字符串、10 个整数和一些我必须存储的 bool 值。有没有更好的方法将数据存储在本地?
我找到了 some code它可以存储一个 ArrayList 但它不适用于自定义类。
编辑:我使用了this tutorial解决我的问题。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import android.os.Environment;
public class SerializeData {
public static byte[] serializeObject(Object o) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(o);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
return buf;
} catch (IOException ioe) {
CreateLog.addToLog(ioe.toString());
return null;
}
}
public static Object deserializeObject(byte[] b) {
try {
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(b));
Object object = in.readObject();
in.close();
return object;
} catch (ClassNotFoundException cnfe) {
CreateLog.addToLog(cnfe.toString());
return null;
} catch (IOException ioe) {
CreateLog.addToLog(ioe.toString());
return null;
}
}
public static void saveData(){
byte[] arrayData = SerializeData
.serializeObject(MyTasks.allCustomers);
BufferedOutputStream bos;
try {
bos = new BufferedOutputStream(new FileOutputStream(
Environment.getExternalStorageDirectory()
+ "/myprogram/myarray.txt"));
bos.write(arrayData);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
CreateLog.addToLog(e.toString());
} catch (IOException e) {
CreateLog.addToLog(e.toString());
}
}
public static ArrayList<Customer> getData(){
File afile = new File(Environment.getExternalStorageDirectory()
+ "/myprogram/myarray.txt");
int size = (int) afile.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(afile));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
CreateLog.addToLog(e.toString());
} catch (IOException e) {
CreateLog.addToLog(e.toString());
}
return (ArrayList<Customer>) SerializeData.deserializeObject(bytes);
}
}
最佳答案
据我了解,Customer 结构将仅在本地读取和写入,并且也由相同或高度相关的程序读取和写入。对于这种情况,我认为 Java 序列化是合适的,因为它添加的代码很少。
让 Customer 实现 Serializable(立即输入 serialVersionUID,以便稍后控制版本)。 ArrayList 已经是可序列化的。按照说明获取文件名here并使用 ObjectInput/Output 流进行序列化。如果需要,您以后可以随时迁移到 XML/JSON。可以找到一个简单的序列化“hello world”示例 here .
关于android - 将 ArrayList 写入 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14304863/