J2MEを使用してモバイルアプリを構築しています。プログラムの実行中にRecordStoreに書き込んだデータにアクセスできますが、プログラムを終了して再起動すると失われます。例外はスローされず、データは単に失われます。
更新:あなたの提案をみんなに感謝します。Windows 7でNetBeansを使用しています。以前にインストールしたWTKバージョンを使用しているか、別の場所にインストールした別のバージョンを使用しているかわかりません。Pavelが書いたファイルのWTKフォルダーを確認しましたが、見つかりませんでした。今、私は自分の電話とエミュレーターの他のすべてで永続性を必要とする機能をテストしていますが、もちろんエミュレーターですべてをテストできる方がはるかに良いでしょう。
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
}
}
}
}