繁体   English   中英

将ArrayList写入SD卡

[英]Write an ArrayList to SD-Card

当我的应用程序启动时,它会连接到数据库并以XML格式下载一些数据。 这是布局:

<Customers>
  <Customer> 
    <name>...</name>
    ...
  </Customer>
   <Customer> 
    <name>...</name>
    ...
  </Customer>
  ...
</Customer>

对于每个客户,我创建一个存储在ArrayList中的类Customer的对象。 用户可以编辑和添加每个客户的一些数据,然后可以将其上传回服务器。

问题是我不知道什么是在本地存储数据的最佳方法,因此,如果我的应用程序关闭或用户不再有互联网,他们将有一个备份。 现在,我将所有Customer对象转换回XML,然后将其存储在SD卡上,但是我认为这不是一个好的解决方案。 每个客户有大约40个String,10个整数和一些我必须存储的布尔值。 有没有更好的方法在本地存储数据?

我发现一些代码可以存储ArrayList,但不适用于自定义类。

编辑:我使用本教程来解决我的问题。

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); 

    }
}

据我了解,客户结构只能在本地以及相同或高度相关的程序中读写。 对于这种情况,我认为Java序列化是合适的,因为它添加了很少的代码。

使客户实现Serializable(立即输入serialVersionUID,以便以后可以控制版本)。 ArrayList已经可序列化。 按照此处的说明获取文件名,然后使用ObjectInput / Output流进行序列化。 如果需要,您以后总是可以迁移到XML / JSON。 可以在此处找到一个简单的序列化“ hello world”示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM