Persisting data on the phone.

Java for Small Spaces
Can Java phone home?
by Chris Carpenter
Listing 1. Persisting data on the phone.


// This method stores the retrieved file in the phone's recordstore
// We are using Vector instead of ArrayList because Vector is the
// only supported collection
public void createRecordStore(Vector fileContents) {
    RecordStore recordStore = null;
    String recordStoreName = new String(fileName);
    int recordID = 0;

    // Delete the old database (if any)
    try {
        RecordStore.deleteRecordStore(recordStoreName);
    } catch (Exception e) {
    }
    try {
        recordStore = RecordStore.openRecordStore(recordStoreName, true);
    } catch (RecordStoreException rse) {
        System.err.println(rse.toString());
    }
    try {
        byte[] incomingBytes;
        Enumeration enumeration = fileContents.elements();
        while (enumeration.hasMoreElements()) {
            incomingBytes = ((String) enumeration.nextElement()).getBytes();
            recordStore.addRecord(incomingBytes, 0, incomingBytes.length);
        }
    } catch (Exception e) {
        System.err.println(e.toString());
    }
    try {
        recordStore.closeRecordStore();
    } catch (RecordStoreException rse) {
        System.err.println(rse.toString());
    }
}

About the Author

Chris Carpenter is a software engineer and architect at RoleModel Software, Inc. in Holly Springs, NC. He can be contacted at [email protected].