Question

I've just started out with J2ME and record stores. This seems to be the proper way to open a record store named "foo", not creating a new one:

RecordStore.openRecordStore("foo", false)

Fine, I get that. But where do I put the actual file for my program to find it? I'm using NetBeans 7.1.2.

Était-ce utile?

La solution

you don't need to know where the file is, J2Me put the file somewhere, if the store already exists, you can open it, or use true in the open method to create it if it doesn't exist.

RecordStore rs = RecordStore.openRecordStore("foo", true);

to write to your recordstore, use this :

String s = "your-data";
byte[] rec = s.getBytes();
rs.addRecord(rec, 0, rec.length);

to read :

RecordEnumeration re = rs.enumerateRecords(null, null, false);
while (re.hasNextElement()){
   String s = new String(re.nextRecord());
}

and close your recordStore after each operation:

 rs.closeRecordStore();

update

how do I read the contents of the file?

read your existing file as a normal file with :

InputStream is = getClass().getResourceAsStream("/res/foo");
StringBuffer sb = new StringBuffer();
int chars;
while ((chars = is.read()) != -1)
    sb.append((char) chars);
String str = new String(String.valueOf(sb).getBytes("UTF-8"));

and write the string to your recordStore with the code above.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top