?? usermodel.java
字號:
package com.hziee.phone_book.model;
import java.io.*;
import javax.microedition.rms.*;
public class UserModel {
private RecordStore accountStore;
public static final String RNAME = "accountstore";
public UserModel() {
try {
accountStore = RecordStore.openRecordStore(RNAME, true);
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public void closeRecordStore() {
try {
accountStore.closeRecordStore();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public void deleteAccount() {
try {
closeRecordStore();
if(accountStore!=null)RecordStore.deleteRecordStore(RNAME);
// accountStore.deleteRecord(11);
// accountStore.deleteRecord(12);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
public void saveAccount(Account account) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
account.serialize();
byte[] data = baos.toByteArray();
accountStore.addRecord(data, 0, data.length);
baos.close();
System.out.println("==="+accountStore.getNumRecords());
} catch (IOException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public Account getAccount(int recordID) {
try {
if (accountStore.getNumRecords() > 0) {
byte[] data = accountStore.getRecord(recordID);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
Account account = Account.deserialize(dis);
bais.close();
return account;
}
return null;
} catch (IOException e) {
return null;
} catch (RecordStoreException e) {
return null;
}
}
public void savePreference(Preference preference) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
// accountStore.closeRecordStore();
preference.serialize(dos);
byte[] data = baos.toByteArray();
int recordId = accountStore.addRecord(data, 0, data.length);
baos.close();
System.out.println("save=========="+recordId+" " + preference);
} catch (IOException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public Preference getPreference(int recordID) {
try {
System.out.println("getPreference:"+accountStore.getNumRecords());
if (accountStore.getNumRecords() > 0) {
byte[] data = accountStore.getRecord(recordID);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
Preference preference = Preference.deserialize(dis);
System.out.println(preference+"next=====:" + accountStore.getNextRecordID());
bais.close();
return preference;
}
return null;
} catch (IOException e) {
return null;
} catch (RecordStoreException e) {
return null;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -