内容摘要:J2ME记录管理系统 ( RMS )允许数据流被储存并且在一个记录基础上访问数据。由应用程序开发者把每个记录解析到字段水平。RMS程序包内部的接口支持一个应用程序定义的基础上的比较与检索功能。
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;
}
}
责编:豆豆技术应用
- Java新闻
- Java入门教程
- Java开发工具
- J2EE
- J2SE
- J2ME
- EJB/Spring
- Applet/Swing
- Servlet/JSP
- Struts/Hibernate
- JDBC/JDO
- Ajax
- 认证考试
- JAVA对象比较器Comparator
- Java 6 RowSet 使用完全剖析
- 深入了解"Java"中的“异常机制”
- 讨论5种跟踪Java执行的方法
- Java专业术语标准化规范
- java中线程概念描述
- JAVA中的反射机制详解
- Java语言灵巧指针与垃圾回收
- 讲述java语言中内部类的研究
- Java对于Cookie的操作详解