RecordStore javadocから: 「MIDlet スイート内の MIDlet は、それぞれに異なる名前が付けられている限り、複数のレコード ストアを作成できます。MIDlet スイートがプラットフォームから削除されると、その MIDlet に関連付けられているすべてのレコード ストアも削除されます。 . MIDlet スイート内の MIDlet は、互いのレコード ストアに直接アクセスできます。"
そのため、MIDlet 内で複数のレコード ストアを操作できます。
以下は、私が時々学生と一緒に使用する例です (警告: この MIDlet には UI はありません: デモンストレーションのみを目的としています)。1 つまたは 2 つの変数を使用しても問題ないことがわかります。
package test;
java.io.ByteArrayInputStream をインポートします。java.io.ByteArrayOutputStream をインポートします。java.io.DataInputStream をインポートします。java.io.DataOutputStream をインポートします。import java.io.IOException; import javax.microedition.midlet.*; import javax.microedition.rms.RecordEnumeration; javax.microedition.rms.RecordStore をインポートします。import javax.microedition.rms.RecordStoreException;
public class ExampleTwoRS extends MIDlet {
private final static String RS_BOYS_NAME = "boys";
private final static String RS_GIRLS_NAME = "girls";
public ExampleTwoRS() {
Person[] people = new Person[4];
people[0] = new Person("Angelina", false);
people[1] = new Person("Brad", true);
people[2] = new Person("Mirka", false);
people[3] = new Person("Roger", true);
try {
initData(people);
readData(RS_BOYS_NAME);
readData(RS_GIRLS_NAME);
} catch (RecordStoreException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void startApp() {
}
private void initData(Person[] people) throws RecordStoreException, IOException {
RecordStore rsBoys = null;
RecordStore rsGirls = null;
try {
rsBoys = RecordStore.openRecordStore(RS_BOYS_NAME, true);
rsGirls = RecordStore.openRecordStore(RS_GIRLS_NAME, true);
for (int i = 0; i < people.length; i++) {
byte[] data = people[i].toByteArray();
if (people[i].isMale()) {
rsBoys.addRecord(data, 0, data.length);
} else {
rsGirls.addRecord(data, 0, data.length);
}
}
} finally {
rsBoys.closeRecordStore();
rsGirls.closeRecordStore();
}
}
private void readData(String rsName) throws RecordStoreException, IOException {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(rsName, true);
int i = 0;
RecordEnumeration re = rs.enumerateRecords(null, null, true);
Person[] people = new Person[re.numRecords()];
while (re.hasNextElement()) {
people[i] = new Person();
people[i].fromByteArray(re.nextRecord());
System.out.println(rsName + ": " + people[i].toString());
i++;
}
} finally {
rs.closeRecordStore();
}
}
private void initNumbers() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class Person {プライベート文字列名; ブール値の男性;
public Person() {
}
public Person(String name, boolean male) {
this.name = name;
this.male = male;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
public void fromDataStream(DataInputStream dis) throws IOException {
this.name = dis.readUTF();
this.male = dis.readBoolean();
}
public void toDataStream(DataOutputStream dos) throws IOException {
dos.writeUTF(getName());
dos.writeBoolean(isMale());
}
public void fromByteArray(byte[] data) throws IOException {
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);
fromDataStream(din);
din.close();
}
public byte[] toByteArray() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
toDataStream(dout);
dout.close();
return bout.toByteArray();
}
public String toString() {
return name + (male ? " (b)" : " (g)");
}
}