?? addressdb.java
字號:
package ch09.section03;
import javax.microedition.rms.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
public class AddressDB {
private static RecordStore rs = null;
public AddressDB() {
try {
rs = RecordStore.openRecordStore("addressbook", true);
}
catch (RecordStoreException e) {
System.out.println(e);
e.printStackTrace();
}
}
//添加一條記錄
public void addAddress(String Name, String Address) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(os);
try {
output.writeUTF(Name + "," + Address);
}
catch (IOException e) {
System.out.println(e);
e.printStackTrace();
}
byte[] b = os.toByteArray();
try {
rs.addRecord(b, 0, b.length);
}
catch (RecordStoreException e) {
System.out.println(e);
e.printStackTrace();
}
}
//獲取指定記錄的姓名部分
public static String getName(int index) {
int counter = 1;
int commalocation = 0;
String name = null;
try {
RecordEnumeration enumRec =
rs.enumerateRecords(null, null, false);
while ( (counter <= index) && (enumRec.hasNextElement())) {
String strTemp = new String(enumRec.nextRecord());
commalocation = strTemp.indexOf(',');
name = strTemp.substring(2, commalocation);
counter++;
}
}
catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return name;
}
//獲取指定記錄的地址部分
public static String getAddress(int index) {
int counter = 1;
int commalocation = 0;
String address = null;
try {
RecordEnumeration enumRec =
rs.enumerateRecords(null, null, false);
while ( (counter <= index) && (enumRec.hasNextElement())) {
String strTemp = new String(enumRec.nextRecord());
commalocation = strTemp.indexOf(',');
address = strTemp.substring(commalocation + 1);
counter++;
}
}
catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return address;
}
//獲取記錄數
public static int recordCount() {
int count = 0;
try {
count = rs.getNumRecords();
}
catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
return count;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -