gpt4 book ai didi

java - RMS 存储持久数据的问题

转载 作者:行者123 更新时间:2023-11-30 11:48:32 26 4
gpt4 key购买 nike

1.模拟器问题:
我第一次启动我的 midlet 应用程序是为了存储一些数据,然后我第二次重新启动它是为了读取存储的数据。它在前两种情况下运行良好,无一异常(exception)。

但是我第二次以相同的方式重新启动它然后它给出异常:“未捕获的异常 java/lang/NumberFormatException:”它仅处理 char 并且总数据小于 64 kb。

2.真机问题:
RMS 根本不工作。我不知道是否需要为手机 (nokia n95) 授予权限。

谢谢。


在应用程序中,它只会根据选定的国家/地区将慈善机构存储到 rms 中。所以如果一个国家已经被选中,它必须跳过国家列表,然后在每次重启时显示公司列表。在下面的代码中,rms_Check() 方法是检查数据以打开国家或公司列表框。

public class X {

private static RecordStore rs =null;
private static Vector rms_Vector = new Vector();
static final String REC_STORE ="db_1";

public X() {

}

public void openRecStore(){
try {
rs = RecordStore.openRecordStore(REC_STORE, true);
System.out.println("open record store");
} catch (Exception e)
{
db(e.toString()+" in openRecStore");
}
}

public void closeRecStore(){
try {
rs.closeRecordStore();
} catch (Exception e) {
db(e.toString()+" in closeRecStore");
}
}

public void deleteRecStore()
{
if (RecordStore.listRecordStores()!=null){
try {
RecordStore.deleteRecordStore(REC_STORE);
} catch (Exception e) {
db(e.toString()+" in deleteRecStore");
}
}
}

public void writeRecord(String str) throws UnsupportedEncodingException
{
byte[] rec = str.getBytes("UTF-8");

try {
rs.addRecord(rec, 0, rec.length);
System.out.println("write record store");
} catch (Exception e) {
db(e.toString()+" in writeRecord");
}
}

public void readRecord()
{
try {
// Intentionally it is too small to test code
byte[] m_enc = new byte[5];
byte[] recData = new String(m_enc).getBytes("UTF-8");
int len;

for(int i =1; i<= rs.getNumRecords(); i++){
if(rs.getRecordSize(i)> recData.length)
recData = new byte[rs.getRecordSize(i)];

len = rs.getRecord(i, recData, 0);
System.out.println("Record #"+i+":"+new String(recData, 0, len));
System.out.println("------------------------");
rms_Vector.addElement(new String(recData, 0, len));
}
} catch (Exception e) {
db(e.toString() +" in readStore");
}
}

private void db(String str)
{
System.out.println("Msg:"+str);
}
public Vector rms_Array(){

return this.rms_Vector;
}

public boolean rms_Check(){
if(this.rms_Vector.size()>0){
System.out.print("rms_check: true");
// if true it will display company list every time
return true;
}else{
System.out.print("rms_check: false");
//if false it will display country list then company list
return false;
}
}

}

最佳答案

使用这个

private RecordStore rs = null;    // Record store
public String REC_STORE = "RSM name"; // Name of record store
public int record_max=0;

public void openRecStore(){
try{
rs = RecordStore.openRecordStore(REC_STORE, true );
}catch (Exception e){}
}

public void closeRecStore(){
try{
rs.closeRecordStore();
}catch (Exception e){}
}

public void deleteRecStore(){
if (RecordStore.listRecordStores() != null){
try{
RecordStore.deleteRecordStore(REC_STORE);
}catch (Exception e){}
}
}

public void writeRecord(String str){
byte[] rec = str.getBytes();

try{
rs.addRecord(rec, 0, rec.length);
}catch (Exception e){}
}

public void readRecords(){
try{
byte[] recData = new byte[5];
int len;
record_max=rs.getNumRecords();
for(int i = 1; i <= record_max; i++){

if(rs.getRecordSize(i) > recData.length){
recData = new byte[rs.getRecordSize(i)];

}
len = rs.getRecord(i, recData, 0);
file_name[i]=new String(recData, 0, len);

}
}catch (Exception e){}
}

你有 file_name[] 保存数据数组

用于加载肌动蛋白命令使用:

            openRecStore();
readRecords();
for(int j=1;j<=record_max;j++ ) {
System.out.println("Record " + j + " : " + file_name[j]);
}
closeRecStore();

并保存:

            openRecStore();
writeRecord(textField.getString());
closeRecStore();

关于java - RMS 存储持久数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8745995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com