我想知道在j2me中是否可以同时打开2个或更多recordStores。我基本上希望能够在相同的代码执行中从2个不同的recordStores中添加/删除记录。这可能吗?
如果是这样,您将如何做?在课程的顶部,您可以执行“ 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)");
}
}
关于java - 是否可以在j2me中同时打开和编辑2个或更多RecordStore?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2553771/