J2ME RecordStore 中的数据不会跨会话持续存在
我正在使用 J2ME 构建一个移动应用程序,我发现我写入 RecordStore 的数据可以在程序仍在运行时访问,但在退出并重新启动后会丢失。没有抛出异常,数据只是丢失了。
更新:感谢大家的建议。我在 Windows 7 上使用 NetBeans。我不确定它是使用我之前安装的 WTK 版本还是在其他地方安装的另一个版本。我检查了我的 WTK 文件夹中是否有 Pavel 所写的文件,但找不到它们。现在我正在测试手机上需要持久性的功能以及模拟器中的其他所有内容,但如果能够在模拟器中测试所有内容当然会更好。
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
}
}
}
}
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
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用 Sun WTK,它会在其“appdb”文件夹中创建一个名为“in.use”的文件:
如果您以不寻常的方式关闭模拟器(例如终止一个进程),它不会删除它,下次您关闭模拟器时,它不会将其删除。运行模拟器,它会创建临时文件夹来存储数据:
以这种方式启动时,它应该在控制台中打印:“Running with storage root temp.DefaultColorPhone1”。
我修复了它,在我的“.bat”文件中包含一行,用于每次模拟器运行时删除“in.use”文件。但同时运行多个模拟器时应该小心。
If you use Sun WTK, it creates a file named "in.use" in its "appdb" folder:
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:
when starting this way, it should print in console: "Running with storage root temp.DefaultColorPhone1".
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.
我自己也遇到了同样的问题,但是我确实发现 NetBeans 或其他什么程序在执行后删除了已部署的程序文件。这些文件位于 C:\Documents and Settings\MyUser\javame-sdk\3.0\work\0\appdb 文件夹中,在 Vista/Win7 上可能有所不同,我猜路径中的数字指的是您当前使用的模拟器使用。无论如何,在此文件夹中查找名称类似于您的 RecordStore 的内容。例如“00000002_PSC_onfig.db”,这是我名为 PSConfig 的套件配置记录存储。通过将其复制到例如“Copy of 00000002_PSC_onfig.db”,它将不会被删除。 NetBeans 清理完毕后,只需将其复制回原来的名称即可。
下次您在 NetBeans 中运行时,您的记录存储就会在那里。这很痛苦,但至少它使您可以使用模拟器来调试 RMS 处理。
I experienced the same problem myself, I did however discover that NetBeans, or whatever, deletes the deployed program files after execution. These files are located in the C:\Documents and Settings\MyUser\javame-sdk\3.0\work\0\appdb folder, might be different on Vista/Win7 and I guess the number in the path refers to the emulator you are currently using. Anyways, in this folder look for something that is named like your RecordStore. E.g. "00000002_PSC_onfig.db", which is my suite configuration recordstore named PSConfig. By copying this to e.g. "Copy of 00000002_PSC_onfig.db" it will not be deleted. After NetBeans have cleaned up, just copy it back to its original name.
The next time you hit run in NetBeans your recordstore will be there. It's pain, but at least it gives you the possibility to use the emulator to debug your RMS handling.
这个问题已经存在了一段时间,但我在寻找模拟器相同问题的答案时偶然发现了它,但就我而言,是在使用 Java ME 3 SDK 时发现的。我找到的解决方案也可能解决这个问题。
使用:
将根据文档:“安装 MIDlet,运行它,完成后卸载它。”
要持久安装(及其数据),您应该使用:
JAD 文件 URL 可以是网址,也可以是“file:///path/to/app.jad”(如果您想从本地文件系统安装)。此安装命令将显示应用程序存储编号,然后您可以使用该编号来启动模拟器并通过调用运行以前安装的应用程序:
请参阅 docs 了解更多命令行选项。
This question has been around for a while but I stumbled upon it whilst looking for an answer to the same problem with the emulator but in my case it was when using the Java ME 3 SDK. It is possible that the solution I found might also fix this problem.
Using:
will according to the docs: "Install a MIDlet, run it, and uninstall it after it finishes."
To persist an installation (and it's data) you should use:
The JAD file URL can either be a web address or 'file:///path/to/app.jad' if you want to install from your local file system. This installation command will display an application storage number which you can then use to launch the emulator and run the previously installed app by calling:
See the docs for further command line options.
我终于可以让它在真正的手机上运行了。看来,正如 Martin Clayton 所建议的那样,模拟器重置删除了数据。不过,我仍在寻找一种在模拟器中启用持久性的方法。
I could finally get it to work on a real handset. It seems that, as Martin Clayton suggested, the emulator reset erased the data. I'm still looking for a way to enable persistence in the emulator though.
如果您使用的是 Windows Vista,则可能且几乎存在权限问题。我不确定如何解决此问题,但您可能需要检查运行模拟器的用户是否有权写入模拟器存储。
在 appdb/$phone/*.db 中
If you are using windows Vista there can and almost are permission issues. I am not sure how to resolve this but you might want to check that the user that is running the emulator has access to write to the emulator store.
In appdb/$phone/*.db
要解决存储问题,您需要检查 netbeans 平台管理器中的“存储大小”选项。
进入项目属性;
进入站台;
管理模拟器;
选择sun java wireless toolkit;
进入“工具和扩展”;
开放偏好;
贮存;
存储大小选项...设置大小。
这对我有用
to fix the storage problem you need to check the option "Storage size" in the netbeans platform manager.
Go in project properties;
go in platform;
manage emulators;
select the sun java wireless toolkit;
go in Tools and Extensions;
open preferences;
storage;
storage size option... set a size.
this works for me
我在 Ubuntu Linux 上使用 WTK 2.5.2 和 Netbeans 8.0.2 时遇到了同样的问题。后来我认为这是由于关闭笔记本电脑而没有关闭模拟器造成的。如果您运行第二个模拟器而不关闭第一个模拟器,也会发生这种情况。
我的解决方案基于此处的最佳答案,只需关闭所有模拟器并删除位于
~/j2mewtk/2.5.2/appdb/DefaultColorPhone
的文件I experienced the same issue on Ubuntu Linux and working with WTK 2.5.2 and Netbeans 8.0.2. I later figured it was caused by shutting down my laptop without closing the emulator. It also happens if you run a second emulator without shutting down the first.
My solution is based on the Best Answer here, just shut down all emulators and delete the file located at
~/j2mewtk/2.5.2/appdb/DefaultColorPhone