📄 stringrecord.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class StringRecord extends MIDlet implements CommandListener{
private Command exitCommand;
private Display display;
private RecordStore rs; // 定义记录存储rs
private TextBox showMsg; // 用于显示操作结果信息
private String msg = null; // 存放用于文本框显示的信息
public StringRecord() {
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 = {"李建成","徐雅琳","杨斌成","胥湘莉"};
String[] sex = {"男","女","男","女"};
String[] phone = {"88974527","84825429","84895629","84871258"};
String[] 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 + ";";
message += "性别:" + fnd.sex;
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, sex, phone, age;
public Friend(){
name = sex = phone = age = "";
}
//将数据格式化成为字节数组, 用分隔符号#来连接
public byte[] encode(){
String rec = name + "#" + sex + "#" + phone + "#" + age;
byte[] data = rec.getBytes();
return data;
}
//通过分隔符号#来将字节数组还原为原始数据
public void decode(byte[] data){
String rec = new String(data);
name = sex = phone = age = "";
int pos1 = rec.indexOf('#');
if (pos1>=0) {
name = rec.substring(0,pos1);
int pos2 = rec.indexOf('#',pos1+1);
if (pos2>pos1) {
sex= rec.substring(pos1+1,pos2);
int pos3 = rec.indexOf('#',pos2+1);
if (pos3>pos2) {
phone = rec.substring(pos2+1,pos3);
age = rec.substring(pos3+1,rec.length());
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -