📄 datarecord.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class DataRecord extends MIDlet implements CommandListener{
private Command exitCommand;
private Display display;
private RecordStore rs; // 定义记录存储rs
private TextBox showMsg; // 用于显示操作结果信息
private String msg = null; // 存放用于文本框显示的信息
public DataRecord() {
display = Display.getDisplay(this);
showMsg = new TextBox("操作信息:", null, 512, TextField.ANY);
exitCommand =new Command("退出",Command.EXIT,1);
showMsg.addCommand(exitCommand);
showMsg.setCommandListener(this);
}
public void startApp(){
if (!OpenRS("tempRS"))
msg = "记录存储tempRS打开失败";
else {
msg = "记录存储tempRS打开成功";
if (AddRec()) {
msg += "\n记录添加成功";
msg += GetRec();
}
else {
msg += "\n记录添加失败";
}
}
showMsg.setString(msg);
display.setCurrent(showMsg);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) {
if (CloseRS())
System.out.println("记录存储tempRS关闭成功");
else
System.out.println("记录存储tempRS关闭失败");
if (DeleteRS("tempRS"))
System.out.println("记录存储tempRS删除成功");
else
System.out.println("记录存储tempRS删除失败");
}
public void commandAction(Command c,Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
// 添加记录
public boolean AddRec() {
String[] name = {"李建成","徐雅琳","杨斌成","胥湘莉"};
boolean[] sex = {true,false,true,false};
String[] phone = {"67897457","84782548","84789569","45871258"};
int[] age = {34,29,30,26};
Friend fnd = new Friend();
for (int i=0; i<4 ; i++) {
fnd.name = name[i];
fnd.sex = sex[i];
fnd.phone = phone[i];
fnd.age = age[i];
byte [] data = fnd.encode();
try {
rs.addRecord(data, 0, data.length);
}
catch (RecordStoreException e) {
return false;
}
}
return true;
}
// 读取所有记录
public String GetRec() {
int id, num;
String message = null;
try {
id = rs.getNextRecordID();
num = rs.getNumRecords();
message = "\n记录存储中共有" + num + "条记录:";
}
catch (RecordStoreException e) {
message = "\n记录读取失败!";
return message;
}
Friend fnd = new Friend();
byte [] data = null;
if (num >0) {
for (int i=1; i<id; i++) {
try {
data = rs.getRecord(i);
fnd.decode(data);
message += "\n记录号:" + i;
message += "\n姓名:" + fnd.name + ";";
if (fnd.sex)
message += "性别:" + "男";
else
message += "性别:" + "女";
message += "\n电话号码:" + fnd.phone + ";";
message += "年龄:" + fnd.age;
}
catch (InvalidRecordIDException e) { }
catch (RecordStoreException e) {
message += "\n记录读取失败!";
}
}
}
return message;
}
// 打开(创建)记录存储
public boolean OpenRS(String rsName) {
try {
rs = RecordStore.openRecordStore(rsName, true);
return true;
}
catch(RecordStoreException e) { }
return false;
}
// 关闭记录存储
public boolean CloseRS() {
if(rs != null) {
try {
rs.closeRecordStore();
rs = null;
return true;
}
catch(RecordStoreException e) { }
}
return false;
}
// 删除记录存储
public boolean DeleteRS(String rsName) {
try {
RecordStore.deleteRecordStore(rsName);
return true;
}
catch(RecordStoreException e) { }
return false;
}
// 定义一个类用于实现联系人数据的转换
class Friend{
public String name, phone;
public boolean sex;
public int age;
public Friend(){
name = phone = "";
sex = false;
age = 0;
}
// 将数据格式化成为字节数组
public byte[] encode(){
byte[] data = null;
try{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeUTF(name);
dout.writeBoolean(sex);
dout.writeUTF(phone);
dout.writeInt(age);
data = bout.toByteArray();
dout.close();
bout.close();
}catch(Exception e){ }
return data;
}
// 将字节数组还原为原始数据
public void decode(byte[] data){
try{
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);
name = din.readUTF();
sex = din.readBoolean();
phone = din.readUTF();
age = din.readInt();
din.close();
bin.close();
}catch(Exception e){ }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -