RMS read-write implementation of J2ME

Source: Internet
Author: User

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!"; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.