【J2ME】保存数据--操作RMS

J2ME保存数据有一些蛋筒,这货不是数据库!

以下是我写的工具类:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.rms.RecordStore;

/**
 * 保存数据
 * 
 * @author Denger
 * 
 */
public class RMSHelper {
	/**
	 * 已经存在该记录
	 */
	public static final int EXISTED = -2;
	/**
	 * 出错
	 */
	public static final int ERROR = -1;

	/**
	 * 读取记录成字符串数组
	 * 
	 * @param b
	 *            记录
	 * @param count
	 *            要转成String的个数
	 * @return
	 */
	public static String[] readRecord2Strings(byte[] b, int count) {
		String[] result = new String[count];
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		DataInputStream dis = new DataInputStream(bais);
		try {
			for (int i = 0; i < count; i++) {
				result[i] = dis.readUTF();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dis.close();
				bais.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 把String数组里的内容写成可以存储的字节数组
	 * 
	 * @param ss
	 */
	private static byte[] writeStrings2RecordBytes(String[] ss) {
		ByteArrayOutputStream baos = null;
		DataOutputStream dos = null;
		byte[] b = null;
		try {
			baos = new ByteArrayOutputStream();
			dos = new DataOutputStream(baos);
			for (int i = 0; i < ss.length; i++) {
				dos.writeUTF(ss[i]);
			}
			b = baos.toByteArray();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				dos.close();
				baos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return b;
	}

	/**
	 * 把记录保存到RMS
	 * 
	 * @param rs
	 * @param ss
	 * @return
	 */
	public static int writeStrings2Record(RecordStore rs, String[] ss) {
		int result = ERROR;
		byte[] bb = writeStrings2RecordBytes(ss);
		try {
			result = rs.addRecord(bb, 0, bb.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static RecordStore openStore(String storeName) {
		RecordStore rs = null;
		try {
			rs = RecordStore.openRecordStore(storeName, true);
			rs.setMode(RecordStore.AUTHMODE_ANY, true);// 共享模式
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 关闭RMS
	 * 
	 * @param rs
	 */
	public static void closeStore(RecordStore rs) {
		if (rs == null) {
			return;
		}
		try {
			rs.closeRecordStore();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除
	 * 
	 * @param storeName
	 */
	public static void deleteStore(String storeName) {
		try {
			RecordStore.deleteRecordStore(storeName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

使用保存方法示例:

	/**
	 * 保存。先存姓名,再存电话号码
	 * 
	 * @param info
	 * @return
	 */
	public int saveHelpPhone(HelpPhone info) {
		int result = RMSHelper.ERROR;
		try {
			rs = RMSHelper.openStore(STORE_NAME);
			RecordEnumeration em = rs.enumerateRecords(null, null, true);
			while (em.hasNextElement()) {
				int recordId = em.nextRecordId();
				byte[] b = rs.getRecord(recordId);
				String[] ss = RMSHelper.readRecord2Strings(b, 1);
				if (ss[0].equals(info.getUserName())) {// 已经存在
					Log.d(TAG, info.getUserName() + " is already exit.");
					return RMSHelper.EXISTED;// 已经存在了
				}
			}
			String[] ss = new String[2];
			ss[0] = info.getUserName();
			ss[1] = info.getPhoneNum();
			result = RMSHelper.writeStrings2Record(rs, ss);
			Log.d(TAG, "addRecord:" + result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Types2Frame.log = e.getMessage();
		} finally {
			try {
				rs.closeRecordStore();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}


删除记录示例:

	public boolean removeHelpPhone(String userName) {
		try {
			rs = RMSHelper.openStore(STORE_NAME);
			RecordEnumeration em = rs.enumerateRecords(null, null, true);
			while (em.hasNextElement()) {
				String[] ss;
				int recordId = em.nextRecordId();
				byte[] b = rs.getRecord(recordId);
				ss = RMSHelper.readRecord2Strings(b, 1);
				if (ss[0].equals(userName)) {
					rs.deleteRecord(recordId);
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.closeRecordStore();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}


 


 

你可能感兴趣的:(【J2ME】保存数据--操作RMS)