Question

When my application starts it connects to a database and downloads some data in XML format. This is the layout:

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

For each Customer I create an Object of the class Customer wicht I store in a ArrayList. The users can edit and add some data of each Customer and they can upload it back to the server.

The problem is that I don't know what's the best way to store the data locally, so if my application closes or the user don't have internet anymore they'll have a backup. I now covert all Customer objects back to XML and then store it on the SD Card, but this isn't a good solution I think. Each customer has about 40 String, 10 integers and a few booleans I have to store. Is there a better way to store the data locally?

I found some code wich can store an ArrayList but it does not work with custom classes.

Edit: I used this tutorial to fix my problem.

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

    }
}
Was it helpful?

Solution

As I understand, the Customer structure will be read and written only locally and also by the same or highly related program. For such situation, I think, Java serialization is appropriate as it adds very little code.

Make the Customer to implement Serializable (put immediately serialVersionUID so you could control versions later). ArrayList is already Serializable. Obtain the file name as explained here and use ObjectInput/Output stream to serialize. You can always migrate to XML/JSON later if the need arises. A simple "hello world" example for serialization can be found here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top