The following code is derived from http://blog.csdn.net/hellogv/, please specify the source.
Before implementing RMS Read and write, the first thing to note is that RMS is incompatible with Chinese characters, in order to solve this first problem, the following two member functions must be written:
Public byte[] Stringtobytes (String Str) {//write byte
byte[] STB = null;
try {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
DataOutputStream dos = new DataOutputStream (BAOs);
Dos.writeutf (STR);
STB = Baos.tobytearray ();
Baos.close ();
Dos.close ();
} catch (Exception e) {
e.printstacktrace ();
}
return STB;
}
Public String bytestostring (byte[] rec) {//Read contents from bytes
bytearrayinputstream bais = new Bytearrayinputstream (rec);
DataInputStream dis = new DataInputStream (Bais);
String Bts=null;
try {
bts=dis.readutf ();
Bais.close ();
Dis.close ();
} catch (Exception e) {
e.printstacktrace ();
}
return BTS;
}
With the above member function, you can then implement RMS read and write:
Public String Read ()//The return value is the last saved record {try{Rms=recordstore.openrecordstore ("XXXX", true,recordstore.authmode_a
Ny,true);//xxxx is the record set int numbertotal =rms.getnumrecords (); if (numbertotal==0)//When the number of recorded phone numbers is zero return "",//return null character return bytestostring (Rms.getrecord (1));//Here only Read a record} catch (Exception e) {return "";} Failed, no information was read} public string Save (string number)//Save numb {try{Rms=recordstore.openrecordstore ("XXXX", t Rue,recordstore.authmode_any,true);//xxxx is the record set byte[] data = stringtobytes (number);//--------------only create a new record or modify only A record if (Rms.getnumrecords () ==0)//no record Rms.addrecord (data, 0, data.length);//create record if (Rms.getnumrec Ords () ==1)//Only the current one record Rms.setrecord (1,data, 0, data.length);//Modify Record Rms.closerecordstore ();//--------------
The above only creates a new record or only changes a record return "successfully replaced the original number/n new number saved successfully!"; } catch (Exception e) {try{RMS. Closerecordstore ();
} catch (Exception ee) {}} return "Cannot save new number!"; }