读取和写入记录存储中的问题 [英] problem in reading and writing into a recordstore

查看:83
本文介绍了读取和写入记录存储中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我编写了具有两个方法的此类,writeToDisk用于写入记录存储,readFromDisk用于从记录存储读取,
但是我不工作,我也不知道为什么
有人可以帮我吗

  public   class  TopList {


    无效 writeToDisk(字符串 recordstr)抛出 IOException {
        尝试 {
            RecordStore ExpenseRecord = RecordStore.openRecordStore(" ,true);
// 创建输出流
            ByteArrayOutputStream baos =  ByteArrayOutputStream();
            DataOutputStream os =  DataOutputStream(baos);
// 要保存到输出流中的文字值
            os.writeUTF(recordstr);
            

// 关闭流
            os.close();

            字节 [] data = baos.toByteArray();

// 将记录写入记录存储
             int  id = ExpenseRecord.addRecord(data, 0 ,data.length);
// 关闭记录存储
            ExpenseRecord.closeRecordStore();
        } 捕获(例如RecordStoreFullException){
            ex.printStackTrace();
        } 捕获(前RecordStoreNotFoundException){
            ex.printStackTrace();
        } 捕获(ex RecordStoreException ex){
            ex.printStackTrace();
        }
    }

    
    字符串 readFromDisk( int  RecordID)抛出 IOException {
        布尔 flag = true;
        字符串 recordstr = null;
        同时(标志){
            尝试 {
               RecordStore ExpenseRecord = RecordStore.openRecordStore(" ,false);

                字节 [] data = ExpenseRecord.getRecord(RecordID);


                标志=假;

                DataInputStream是=  DataInputStream( ByteArrayInputStream(data));



                 recordstr = is.readUTF();
                


                is.close();
                ExpenseRecord.closeRecordStore();
            } 捕获(例如RecordStoreFullException){
                ex.printStackTrace();
            } 捕获(前RecordStoreNotFoundException){

                ex.printStackTrace();
            } 捕获(例如,InvalidRecordIDException){

                
            } 捕获(ex RecordStoreException ex){
                ex.printStackTrace();
            }
        }
        返回 recordstr;
        
        
    }

} 

解决方案

公共字符串[] readDeviceDetailById(int id,RecordStore rcs){
字符串devId = null;
字符串SwitchID = null;
字符串devStatusOnOff = null;
字符串ru = null;
字符串timer = null;
字符串出现次数=空;
字符串onTime = null;
字符串aOn = null;
字符串offTime = null;
字符串aOff = null;
字符串devtag = null;
字符串pic = null;
字符串Swtag = null;
尝试{
byte [] byteInputData =新的byte [100];
ByteArrayInputStream dInput =新的ByteArrayInputStream(byteInputData);

DataInputStream inputDataStream =新的DataInputStream(dInput);
rcs.getRecord(id,byteInputData,0);

devId = inputDataStream.readUTF();
SwitchID = inputDataStream.readUTF();
devStatusOnOff = inputDataStream.readUTF();
ru = inputDataStream.readUTF();
timer = inputDataStream.readUTF();
发生= inputDataStream.readUTF();
onTime = inputDataStream.readUTF();
aOn = inputDataStream.readUTF();
offTime = inputDataStream.readUTF();
aOff = inputDataStream.readUTF();
图片= inputDataStream.readUTF();
devtag = inputDataStream.readUTF();
Swtag = inputDataStream.readUTF();
dInput.close();
inputDataStream.close();

} catch(IOException ex){
//ex.printStackTrace();

Dialog.show("Exception","IO Exception!" + ex.toString(),"OK",null);

} catch(RecordStoreNotFoundException ex){
//ex.printStackTrace();
Dialog.show("Exception",找不到记录存储异常!" + ex.toString(),"OK",null);
} catch(RecordStoreException ex){
//ex.printStackTrace();
Dialog.show("Exception","Record Store Exception!" + ex.toString(),"OK",null);
}

返回新的String [] {devId,SwitchID,devStatusOnOff,ru,timer,occurrance,onTime,aOn,offTime,aOff,pic,devtag,Swtag};
}

这是一项完整的工作,将其传递给RecordStore对象和一条记录的ID,然后可以正常工作了,它已经过测试

感谢n问候
Om Parkash


hi i write this class that have two methods,writeToDisk for write into recordstore and readFromDisk to read from a recordstore,
but i doesn''t work and i don''t know why
can anybody help me

public class TopList {


    void writeToDisk(String recordstr) throws IOException {
        try {
            RecordStore ExpenseRecord = RecordStore.openRecordStore("TopScore", true);
//Creating output streams
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DataOutputStream os = new DataOutputStream(baos);
//Writing value to be saved to output stream
            os.writeUTF(recordstr);
            

//close stream
            os.close();

            byte[] data = baos.toByteArray();

//Write the record to the record store
            int id = ExpenseRecord.addRecord(data, 0, data.length);
//Close Record store
            ExpenseRecord.closeRecordStore();
        } catch (RecordStoreFullException ex) {
            ex.printStackTrace();
        } catch (RecordStoreNotFoundException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    
    String readFromDisk(int RecordID) throws IOException {
        boolean flag = true;
        String recordstr=null;
        while (flag) {
            try {
               RecordStore ExpenseRecord = RecordStore.openRecordStore("TopScore", false);

                byte[] data = ExpenseRecord.getRecord(RecordID);


                flag = false;

                DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));



                 recordstr = is.readUTF();
                


                is.close();
                ExpenseRecord.closeRecordStore();
            } catch (RecordStoreFullException ex) {
                ex.printStackTrace();
            } catch (RecordStoreNotFoundException ex) {

                ex.printStackTrace();
            } catch (InvalidRecordIDException ex) {

                
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
        }
        return recordstr;
        
        
    }

}

解决方案

public String[] readDeviceDetailById(int id,RecordStore rcs){
String devId=null;
String SwitchID = null;
String devStatusOnOff=null;
String ru=null;
String timer=null;
String occurance=null;
String onTime=null;
String aOn=null;
String offTime=null;
String aOff=null;
String devtag=null;
String pic=null;
String Swtag = null;
try {
byte[] byteInputData = new byte[100];
ByteArrayInputStream dInput = new ByteArrayInputStream(byteInputData);

DataInputStream inputDataStream = new DataInputStream(dInput);
rcs.getRecord(id,byteInputData,0) ;

devId = inputDataStream.readUTF();
SwitchID = inputDataStream.readUTF();
devStatusOnOff = inputDataStream.readUTF();
ru = inputDataStream.readUTF();
timer=inputDataStream.readUTF();
occurance = inputDataStream.readUTF();
onTime = inputDataStream.readUTF();
aOn = inputDataStream.readUTF();
offTime = inputDataStream.readUTF();
aOff = inputDataStream.readUTF();
pic = inputDataStream.readUTF();
devtag=inputDataStream.readUTF();
Swtag = inputDataStream.readUTF();
dInput.close();
inputDataStream.close();

} catch (IOException ex) {
//ex.printStackTrace();

Dialog.show("Exception","IO Exception!"+ex.toString(),"OK", null);

} catch (RecordStoreNotFoundException ex) {
//ex.printStackTrace();
Dialog.show("Exception","Record Store Not Found Exception!"+ex.toString(),"OK", null);
} catch (RecordStoreException ex) {
//ex.printStackTrace();
Dialog.show("Exception","Record Store Exception!"+ex.toString(),"OK", null);
}

return new String[]{devId,SwitchID,devStatusOnOff,ru,timer,occurance,onTime,aOn,offTime,aOff,pic,devtag,Swtag};
}

hi This is fully work pass it to a RecordStore object and Id of a record then it works fine it is tested

thanks n regards
Om Parkash


这篇关于读取和写入记录存储中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆