您的位置:首页 > 其它

J2ME road——J2ME实现RMS手机的存储

2010-04-02 17:32 204 查看
package src;
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class SimpleRMS extends MIDlet{

private RecordStore rs;
private static final String STORE_NAME = "My Record Store";

public SimpleRMS() throws Exception
{
rs = RecordStore.openRecordStore(STORE_NAME, true);
//Create some records in the store
String[] words = {"they", "mostly", "come", "at", "night" };
for(int i=0; i<words.length; i++)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream= new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeUTF(words[i]);

//add another dataOutputStream.writeXXX statements if you like
dataOutputStream.flush();


//add the record
byte[] recordOut =byteArrayOutputStream.toByteArray();
int newRecordId = rs.addRecord(recordOut, 0, recordOut.length);
System.out.println("Adding new record;"+ newRecordId + "value:" +recordOut);

dataOutputStream.close();
byteArrayOutputStream.close();
}

System.out.println("Record store now has" +rs.getNumRecords() +
"record(s) using" +rs.getSize() +"byte(s)"+
"[" +rs.getSizeAvailable() +"bytes free]");
//retrieve the records
for(int i =1;i<= rs.getNumRecords(); i++)
{
int recordSize = rs.getRecordSize(i);
if(recordSize >0)
{
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(rs.getRecord(i));
DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);

String value = dataInputStream.readUTF();

System.out.println("Retrieved record;" + i+ "value:" +value);
dataInputStream.close();
byteArrayInputStream.close();
}

}

}

protected void destroyApp(boolean b) throws MIDletStateChangeException {
// TODO 自动生成方法存根

}

protected void pauseApp() {
// TODO 自动生成方法存根

}

protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
destroyApp(false);
notifyDestroyed();
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: