⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 retrievers.java

📁 很齐全通讯录
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class RetrieveRS extends MIDlet implements CommandListener{
	private Command exitCmd, enumCmd, filtCmd, compCmd;
	private Display display;
	private RecordStore rs;		// 定义记录存储rs
	private TextBox showMsg;	// 用于显示操作结果信息
	private String msg = null;	// 存放用于文本框显示的信息
	public RetrieveRS() {
		display = Display.getDisplay(this);
		showMsg = new TextBox("操作信息:", null, 512, TextField.ANY);
		exitCmd =new Command("退出",Command.EXIT,1);
		enumCmd =new Command("遍历",Command.EXIT,1);
		filtCmd =new Command("查询",Command.EXIT,1);
		compCmd =new Command("排序",Command.EXIT,1);
		showMsg.addCommand(exitCmd);
		showMsg.addCommand(enumCmd);
		showMsg.addCommand(filtCmd);
		showMsg.addCommand(compCmd);
		showMsg.setCommandListener(this);
	}
	public void startApp(){
		if (!OpenRS("tempRS"))
			msg = "记录存储tempRS打开失败";
		else {
			msg = "记录存储tempRS打开成功";
			if (AddRec()) {
				msg += "\n记录添加成功";
			}
			else {
				msg += "\n记录添加失败";
			}
		}
		showMsg.setString(msg);
 		display.setCurrent(showMsg);
   }
	public void pauseApp() { }
	public void destroyApp(boolean unconditional) {
		if (CloseRS())
			System.out.println("记录存储tempRS关闭成功");
		else
			System.out.println("记录存储tempRS关闭失败");
		if (DeleteRS("tempRS"))
			System.out.println("记录存储tempRS删除成功");
		else
			System.out.println("记录存储tempRS删除失败");
	}
	public void commandAction(Command c,Displayable d) {
		if (c == exitCmd) {
			destroyApp(false);
			notifyDestroyed();
		}
		else if (c == enumCmd) {
			try	{
				// 定义枚举器,其中包含记录存储中所有记录,顺序未定
				RecordEnumeration re = rs.enumerateRecords(null, null, false);
				msg = GetRec(re);	// 得到枚举器中所有记录
			}
			catch (RecordStoreException e) { }
			showMsg.setString(msg);
		}
		else if (c == filtCmd) {
			try	{
				RecordFilter rf = new MyFilter();
				// 定义枚举器,其中包含年龄在30以上的记录
				RecordEnumeration re = rs.enumerateRecords(rf, null, false);
				msg = GetRec(re);
			}
			catch (RecordStoreException e) { }
			showMsg.setString(msg);
		}
		else if (c == compCmd) {
			try	{
				RecordComparator rc = new MyComparator();
				// 定义枚举器,其中的记录按年龄降序排列
				RecordEnumeration re = rs.enumerateRecords(null, rc, false);
				msg = GetRec(re);
			}
			catch (RecordStoreException e) { }
			showMsg.setString(msg);
		}
	}
	// 添加记录
    public boolean AddRec() {
		String[] name = {"李建成","徐雅琳","杨斌成","胥湘莉"};
		boolean[] sex = {true,false,true,false};
		String[] phone = {"67897457","84782548","84789569","45871258"};
		int[] age = {34,29,30,26};
		Friend fnd =  new Friend();
		for (int i=0; i<4 ; i++) {
			fnd.name = name[i];
			fnd.sex = sex[i];
			fnd.phone = phone[i];
			fnd.age = age[i];
			byte [] data = fnd.encode();
			try {
				rs.addRecord(data, 0, data.length);
			}
			catch (RecordStoreException e) {
				return false;
			}
		}
		return true;
    }
    // 读取枚举器中所有记录
    public String GetRec(RecordEnumeration re) {
		String message = null;
		try {
			int num = re.numRecords();
			message = "枚举器中共有" + num + "条记录:";
			Friend fnd =  new Friend();
			byte [] data = null;
			int count = 0;
			while(re.hasNextElement()){
				count ++;
				data = re.nextRecord();
				fnd.decode(data);
				message += "\n第 " + count + " 条记录:";
				message += "\n姓名:" + fnd.name + ";";
				if (fnd.sex)
					message += "性别:" + "男";
				else
					message += "性别:" + "女";
				message += "\n电话号码:" + fnd.phone + ";";
				message += "年龄:" + fnd.age;
			}
		}
		catch (RecordStoreException e) {
					message = "记录读取失败!";
		}
        return message;
    }
	// 打开(创建)记录存储
	public boolean OpenRS(String rsName) {
		try {
			rs = RecordStore.openRecordStore(rsName, true);
			return true;
		}
		catch(RecordStoreException e) { }
		return false;
	}
	// 关闭记录存储
	public boolean CloseRS() {
		if(rs != null) {
			try {
				rs.closeRecordStore();
				rs = null;
				return true;
			}
			catch(RecordStoreException e) { }
		}
		return false;
	}
    // 删除记录存储
    public boolean DeleteRS(String rsName) {
        try {
            RecordStore.deleteRecordStore(rsName);
            return true;
        }
        catch(RecordStoreException e) { }
        return false;
    }
}
// 定义过滤器,实现记录的查询
class MyFilter implements RecordFilter{
	public boolean matches(byte[] candidate){
		Friend fnd = new Friend();
		fnd.decode(candidate);
		// 匹配规则为年龄在30岁以上(含30岁)
		if (fnd.age >= 30)
			return true;
		else
			return false;
	}
}
// 定义比较器,实现记录的排序
class MyComparator implements RecordComparator{
	public int compare(byte[] rec1, byte[] rec2){
		Friend fnd = new Friend();
		fnd.decode(rec1);
		int age1 = fnd.age;
		fnd.decode(rec2);
		int age2 = fnd.age;
		// 按年龄大小降序排列
		if (age1 > age2 )
			return RecordComparator.PRECEDES;
		else if (age1 < age2)
			return RecordComparator.FOLLOWS;
		else
			return RecordComparator.EQUIVALENT;
	}
}
// 定义一个类用于实现联系人数据的转换
class Friend{
	public String name, phone;
	public boolean sex;
	public int age;
	public Friend(){
		name = phone = "";
		sex = false;
		age = 0;
	}
	// 将数据格式化成为字节数组
	public byte[] encode(){
		byte[] data = null;
		try{
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			DataOutputStream dout = new DataOutputStream(bout);
			dout.writeUTF(name);
			dout.writeBoolean(sex);
			dout.writeUTF(phone);
			dout.writeInt(age);
			data = bout.toByteArray();
			dout.close();
			bout.close();				
		}catch(Exception e){ }
		return data;
	}
	// 将字节数组还原为原始数据
	public void decode(byte[] data){
		try{
			ByteArrayInputStream bin = new ByteArrayInputStream(data);
			DataInputStream din = new DataInputStream(bin);
			name = din.readUTF();
			sex = din.readBoolean();
			phone = din.readUTF();
			age = din.readInt();
			din.close();
			bin.close();
		}catch(Exception e){ }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -