开发者

What data structure for reading / storing 500 objects

When my application starts I need to load about 500 objects from an xml page, like this:

<object>
<name>objectname</name>
<info>info</info>
<info2>info</info2>
<info3>info</info3>
<info4>info</info4>
<alias>false</alias>
</object>

Now I want to store this on the device, hoping the reading will be faster. Currently I use an ObjectOutputStream to write the objects.

private static void write(ArrayList<MyObject> objects, String fileName, Context context) {
        final File cacheDir = context.getCacheDir();
        final File objectsFile = new File(cacheDir.getAbsoluteFile() + File.separator + fileName);

        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        boolean keep = true;

        try {
            fos = new FileOutputStream(objectsFile);
            oos = new ObjectOutputStream(fos);

            oos.writeObject(objects);
        } catch (Exception e) {
            e.printStackTrace();
            keep = false;
        } finally {
            try {
                if (oos开发者_高级运维 != null)
                    oos.close();
                if (fos != null)
                    fos.close();
                if (keep == false)
                    objectsFile.delete();
            } catch (Exception e) {
            }
        }
    }

This is not a very fast solution, reading can take about 10-15 seconds. I'm showing the objects in a listview, so all objects need to be read in at once.

Any ideas?


I think the best method to store such data would be in a database (see here).

Parse once and store the information in the database (one column for each attribute). It should be pretty fast to retrieve 500 records from the database :)


I'm assuming that you don't want to parse the XML each time you run the app. I'd suggest using a DataOutputStream and do your own encoding/decoding. It will be much faster than ObjectOutputStream. You can start the file by writing a count of the number of objects, as that will make reading everything back in a bit easier.

private static void write(ArrayList<MyObject> objects /* , other args */ ) {
    // ...
    try {
        dos = new DataOutputStream(new FileOutputStream(objectsFile));
        dos.writeInt(objects.size());
        for (MyObject object : objects) {
            dos.writeUTF(object.info);
            dos.writeUTF(object.info2);
            dos.writeUTF(object.info3);
            dos.writeUTF(object.info4);
            dos.writeBoolean(object.alias);
        }
        dos.flush();
    } catch (Exception e) {
        e.printStackTrace();
        keep = false;
    } finally {
        try {
            if (dos != null)
                dos.close();
            if (keep == false)
                objectsFile.delete();
        } catch (Exception e) {
        }
    }
}

For reading, just follow the same pattern with a DataInputStream:

ArrayList<MyObject> read( /* args */ ) {
    // ...
    ArrayList<MyObject> objects = new ArrayList<MyObject>();
    int n = dis.readInt();
    while (n-- > 0) {
        MyObject object = new MyObject();
        object.info = dis.readUTF();
        // etc.
        objects.add(object);
    }
    return objects;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜