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; } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |