我想知道在 j2me 中是否可以同时打开 2 个或更多的记录存储。我基本上希望能够在代码的同一执行中从 2 个不同的记录存储中添加/删除记录。这可能吗?
如果是这样,你会怎么做?在类的顶部,您执行类似“private RecordStore rs;”之类的操作。您是否需要有两个这样的实例才能使其工作,或者您可以使用一个声明来做到这一点?
提前致谢。
来自RecordStore javadoc:“MIDlet 套件中的 MIDlet 可以创建多个记录存储,只要它们各自被赋予不同的名称。当 MIDlet 套件从平台中删除时,与其 MIDlet 关联的所有记录存储也将被删除. MIDlet 套件中的 MIDlet 可以直接访问彼此的记录存储。”
因此,您可以在一个 MIDlet 中操作多个记录存储。
这是我有时与学生一起使用的示例(警告:此 MIDlet 中没有 UI:仅用于演示目的)。您可以看到使用一两个变量并不重要。
package test;
导入 java.io.ByteArrayInputStream;导入 java.io.ByteArrayOutputStream;导入 java.io.DataInputStream;导入 java.io.DataOutputStream;导入 java.io.IOException;导入 javax.microedition.midlet.*;导入 javax.microedition.rms.RecordEnumeration;导入 javax.microedition.rms.RecordStore;导入 javax.microedition.rms.RecordStoreException;
公共类 ExampleTwoRS 扩展 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) {
}
}
类人{私人字符串名称;布尔男性;
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)");
}
}