📄 contactdb.java
字号:
import javax.microedition.rms.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;
public class ContactDB {
RecordStore recordStore = null;
public ContactDB(String name) {
// Open the record store using the specified name
try {
recordStore = open(name);
}
catch(RecordStoreException e) {
e.printStackTrace();
}
}
public RecordStore open(String fileName) throws RecordStoreException {
return RecordStore.openRecordStore(fileName, true);
}
public void close() throws RecordStoreNotOpenException,
RecordStoreException {
// If the record store is empty, delete the file
if (recordStore.getNumRecords() == 0) {
String fileName = recordStore.getName();
recordStore.closeRecordStore();
recordStore.deleteRecordStore(fileName);
}
else {
// Otherwise, close the record store
recordStore.closeRecordStore();
}
}
public Contact getContactRecord(int id) {
// Get the contact record from the record store
try {
return (new Contact(recordStore.getRecord(id)));
}
catch (RecordStoreException e) {
e.printStackTrace();
}
return null;
}
public synchronized void setContactRecord(int id, String record) {
// Convert the string record to an array of bytes
byte[] bytes = record.getBytes();
// Set the record in the record store
try {
recordStore.setRecord(id, bytes, 0, bytes.length);
}
catch (RecordStoreException e) {
e.printStackTrace();
}
}
public synchronized int addContactRecord(String record) {
// Convert the string record to an array of bytes
byte[] bytes = record.getBytes();
// Add the byte array to the record store
try {
return recordStore.addRecord(bytes, 0, bytes.length);
}
catch (RecordStoreException e) {
e.printStackTrace();
}
return -1;
}
public synchronized void deleteContactRecord(int id) {
// Delete the contact record from the record store
try {
recordStore.deleteRecord(id);
}
catch (RecordStoreException e) {
e.printStackTrace();
}
}
public synchronized RecordEnumeration enumerateContactRecords()
throws RecordStoreNotOpenException {
return recordStore.enumerateRecords(null, null, false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -