Data in J2ME RecordStore does not persist across sessions

Posted by Botond Balázs on Stack Overflow See other posts from Stack Overflow or by Botond Balázs
Published on 2009-11-09T22:51:47Z Indexed on 2010/04/03 20:43 UTC
Read the original article Hit count: 430

Filed under:
|
|

I'm building a mobile app with J2ME, and I've found that the data I write into a RecordStore can be accessed while the program is still running but it is lost after quitting and restarting it. No exception is thrown, the data is simply lost.

UPDATE: Thanks everyone for your suggestions. I'm using NetBeans on Windows 7. I'm not sure if it is using the WTK version I have previously installed or another one it has installed somewhere else. I've checked my WTK folder for the files Pavel wrote about, but couldn't find them. Now I'm testing the features requiring persistence on my phone and everything else in the emulator, but it would of course be much better to be able to test everything in the emulator.

private RecordStore recordStore = null;

public MyMIDlet() {
    readStuff(); // output: nothing found in recordStore :(
    saveStuff();
    readStuff(); // output: stuff
}

private void readStuff() {
    try {
        recordStore = RecordStore.openRecordStore(REC_STORE, true);
        int n = recordStore.getNumRecords();
        String stuff;

        if (n == 0) {
            stuff = "nothing found in recordStore :(";
        }
        else {
            stuff = new String(recordStore.getRecord(1));
        }

        System.out.println(stuff);
    }
    catch (Exception e) {
        System.out.println("Exception occured in readStuff: " + e.getMessage());
    }
    finally {
        if (recordStore != null) {
            try {
                recordStore.closeRecordStore();
            }
            catch (Exception e) {
                // ignore
            }
        }
    }
}

private void saveStuff() {
    try {
        recordStore = RecordStore.openRecordStore(REC_STORE, true);
        int n = recordStore.getNumRecords();
        byte[] stuff = "stuff".getBytes();
        recordStore.addRecord(stuff, 0, stuff.length);
    } catch (Exception e) {
        System.out.println("Exception occured in saveStuff: " + e.getMessage());
    } finally {
        if (recordStore != null) {
            try {
                recordStore.closeRecordStore();
            } catch (Exception e) {
                // ignore
            }
        }
    }
}

© Stack Overflow or respective owner

Related posts about java-me

Related posts about recordstore