?? multifieldsmidlet.java
字號(hào):
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class MultiFieldsMIDlet extends MIDlet
implements CommandListener {
private Command exitCommand;
private Display display;
private String[] names ={"黃聰明","黃鎮(zhèn)樟","鄭雅文"};
public MultiFieldsMIDlet() {
display = Display.getDisplay(this);
exitCommand = new Command("離開(kāi)", Command.EXIT, 1);
}
public void startApp() {
TextBox aTextBox = new TextBox("主畫(huà)面",null,256,TextField.ANY);
RecordStore rs = null;
byte[] nameEmail=null;
boolean existingOrNot = false;
boolean OK = true;
existingOrNot = existing("aRS3");
if(existingOrNot){
try{
rs = RecordStore.openRecordStore("aRS3",false);
}
catch(Exception e){
OK = false;
}
finally{
if(OK){
aTextBox.setString("aRS3已存在并且已打開(kāi)");
}
else{
aTextBox.setString("aRS3已存在但無(wú)法打開(kāi)");
}
}
}
else{
try{
rs = RecordStore.openRecordStore("aRS3",true);
}
catch(Exception e){
OK = false;
}
finally{
if(OK){
aTextBox.setString("aRS3雖未存在,但已創(chuàng)建并完成打開(kāi)的操作!");
}
else{
aTextBox.setString("aRS3雖未存在,但無(wú)法創(chuàng)建,打開(kāi)文件失?。?quot;);
}
}
}
Person2 aPerson = null;
if(OK)
try{
for(int i=0;i<names.length;i++){
aPerson = new Person2();
aPerson.write(names[i],"login@ms16.hinet.net",36);
byte[] data = aPerson.changeToByteArray();
int recordID = aPerson.getRecordID();
if(recordID != -1){
rs.setRecord(recordID,data,0,data.length);
}
else{
recordID = rs.addRecord(data,0,data.length);
aPerson.setRecordID(recordID);
}
aPerson = null;
}
aTextBox.setString("已完成新增記錄的操作!");
}
catch(Exception e){
aTextBox.setString("新增記錄的操作失敗!");
}
String result="";
aPerson = new Person2();
if(OK)
try{
int number = rs.getNumRecords();
byte[] data;
for(int i =1;i< rs.getNextRecordID();i++){
data = rs.getRecord(i);
aPerson.changeFromByteArray(data);
result += "第" + i + "筆\n" +
"姓名:" + aPerson.getName() + "\n" +
"E-mail:" + aPerson.getEMail() + "\n" +
"年齡:" + aPerson.getAge() + "\n";
}
result += "共有" + number + "筆記錄:\n";
aTextBox.setString(result);
}
catch(Exception e){
aTextBox.setString("獲取記錄的操作失??!" );
try{
rs.closeRecordStore();
System.out.println("1.Closed.");
RecordStore.deleteRecordStore("aRS3");
System.out.println("delete OK");
}
catch(Exception x){
}
}
finally{
try{
if(rs != null) rs.closeRecordStore();
rs.deleteRecordStore("aRS3");
}
catch(Exception e){
}
}
aTextBox.setString(result);
aTextBox.addCommand(exitCommand);
aTextBox.setCommandListener(this);
display.setCurrent(aTextBox);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public boolean existing(String recordStoreName) {
boolean existingOrNot = false;
RecordStore rs = null;
if(recordStoreName.length() > 32) return false;
try{
rs = RecordStore.openRecordStore(recordStoreName,false);
}
catch(RecordStoreNotFoundException e){
existingOrNot = false;
}
catch(Exception e){
}
finally{
try{
rs.closeRecordStore();
}
catch(Exception e){
}
}
return existingOrNot;
}
public void commandAction(Command c, Displayable s) {
destroyApp(false);
notifyDestroyed();
}
}
class Person2{
private int ID = -1;
private String name;
private String EMail;
private int age;
public void write(String name, String EMail, int age){
this.name = name;
this.EMail = EMail;
this.age = age;
}
public void setRecordID(int ID){
this.ID = ID;
}
public int getRecordID(){
return ID;
}
public byte[] changeToByteArray(){
byte[] data = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(name);
dos.writeUTF(EMail);
dos.writeInt(age);
data = baos.toByteArray();
baos.close();
dos.close();
}
catch(Exception e){
}
return data;
}
public void changeFromByteArray(byte[] data){
try{
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
name = dis.readUTF();
EMail = dis.readUTF();
age = dis.readInt();
bais.close();
dis.close();
}
catch(Exception e){
}
}
public String getName(){
return name;
}
public String getEMail(){
return EMail;
}
public int getAge(){
return age;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -