import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Date;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
public class Exec extends MIDlet {
private Display display;
Word word;
Word[] words;
private Form mainForm;
public Exec() {
mainForm = new Form("RMS基本操作");
handlingRMS();
for (int i = 0; i < words.length; i++) {
mainForm.append(words.toString()+"\n");
}
display = Display.getDisplay(this);
}
public void startApp() {
display.setCurrent(mainForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void handlingRMS() {
try {
RecordStore rs = null;
rs = RecordStore.openRecordStore("WordsRMS", true);
word = new Word("NULL", "NULL", (new Date()).getTime(), "NULL");
byte[] data = word.serialize();
rs.addRecord(data, 0, data.length);
rs.closeRecordStore();
rs = RecordStore.openRecordStore("WordsRMS", false);
RecordEnumeration re = rs.enumerateRecords(null, null, false);
words = new Word[re.numRecords()];
int i = 0;
while (re.hasNextElement()) {
byte[] tmp = re.nextRecord();
words = Word.deserialize(tmp);
i++;
}
rs.closeRecordStore();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class Word {
private String enWord;
private String cnWord;
private long dateTime;
private String detail;
public Word() {
}
public Word(String enWord, String cnWord, long dateTime, String detail) {
setEnWord(enWord);
setCnWord(cnWord);
setDateTime(dateTime);
setDetail(detail);
}
public String getEnWord() {
return enWord;
}
public void setEnWord(String enWord) {
this.enWord = enWord;
}
public String getCnWord() {
return cnWord;
}
public void setCnWord(String cnWord) {
this.cnWord = cnWord;
}
public long getDateTime() {
return dateTime;
}
public void setDateTime(long dateTime) {
this.dateTime = dateTime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public byte[] serialize() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(this.enWord);
dos.writeUTF(this.cnWord);
dos.writeLong(this.dateTime);
dos.writeUTF(this.detail);
baos.close();
dos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return baos.toByteArray();
}
public static Word deserialize(byte[] data) {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
Word word = new Word();
try {
word.enWord = dis.readUTF();
word.cnWord = dis.readUTF();
word.dateTime = dis.readLong();
word.detail = dis.readUTF();
bais.close();
dis.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return word;
}
public String toString() {
return enWord + ":" + cnWord + ":" + new Date(dateTime) + ":" + detail;
}
}
[此贴子已经被作者于2010-12-12 18:38:38编辑过]