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