J2ME RecordStore中的数据不会跨会话持续存在 [英] Data in J2ME RecordStore does not persist across sessions

查看:195
本文介绍了J2ME RecordStore中的数据不会跨会话持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用J2ME构建一个移动应用程序,我发现在程序仍在运行时可以访问我写入RecordStore的数据,但是在退出并重新启动它后会丢失。没有例外,数据就丢失了。

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.

更新:感谢大家的建议。我在Windows 7上使用NetBeans。我不确定它是否使用我之前安装的WTK版本或其他安装在其他地方的版本。我已经检查了我的WTK文件夹中Pavel写的文件,但找不到它们。现在我正在测试我的手机和模拟器中的所有其他东西需要持久性的功能,但是能够在模拟器中测试所有内容当然会好得多。

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
            }
        }
    }
}


推荐答案

如果您使用Sun WTK,它会在其appdb文件夹中创建一个名为in.use的文件:

If you use Sun WTK, it creates a file named "in.use" in its "appdb" folder:


C:\WTK25\appdb\DefaultColorPhone\in.use

如果以异常方式关闭模拟器(例如,终止进程),它将不会删除它,并且下次运行模拟器时,它将创建用于存储数据的临时文件夹: / p>

If you close your emulator in unusual way (kill a process, for example), it would not delete it, and next time you run emulator, it would create temporary folder for storing data:


C:\WTK25\appdb\temp.DefaultColorPhone1

以这种方式启动时,它应该在控制台中打印:使用存储根temp.DefaultColorPhone1运行。

when starting this way, it should print in console: "Running with storage root temp.DefaultColorPhone1".

我修复它,包括进入我的 .bat每次都提交一行删除in.use文件,模拟器运行。但是在一次运行多个模拟器时应该小心。

I fix it, including into my ".bat" file a line for deleting "in.use" file each time, emulator runs. But you should be careful when running several emulators at once.

这篇关于J2ME RecordStore中的数据不会跨会话持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆