J2ME中的rms在这的用途:
在进行这个行情软件开发的时候,在开发行情列表的时候,
用户可以定制自己要显示的字段("代码","昨收","最高","最低","成交量","成交额","振幅","今开"),我让程序默认的显示了("最新","涨幅","涨跌"),并且还对这些字段要用到按照一定的顺序进行排列,我用到了RMS那进行开发。我将这些字段都是用类的形式储存到rms中
实例类:
public class ShowQuotesCategory {
private int index;
private String name;
public ShowQuotesCategory(){
}
public ShowQuotesCategory(int index,String name){
this.index=index;
this.name=name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] object2ByteArray() throws Exception{
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeInt(this.index);
dos.writeUTF(this.name);
baos.close();
dos.close();
return baos.toByteArray();
}
public static ShowQuotesCategory byteArray2Object(byte[] b) throws Exception{
ByteArrayInputStream bais=new ByteArrayInputStream(b);
DataInputStream dis=new DataInputStream(bais);
ShowQuotesCategory fenshiCategory=new ShowQuotesCategory();
fenshiCategory.setIndex(dis.readInt());
fenshiCategory.setName(dis.readUTF());
bais.close();
dis.close();
return fenshiCategory;
}
}
下面的为对字段进行排序的类:
public class ShowQuotesCategoryComparator implements RecordComparator {
public int compare(byte[] b1, byte[] b2) {
try {
ShowQuotesCategory f1=ShowQuotesCategory.byteArray2Object(b1);
ShowQuotesCategory f2=ShowQuotesCategory.byteArray2Object(b2);
if(f1.getIndex()>f2.getIndex()){
return RecordComparator.FOLLOWS;
}else if(f1.getIndex()<f2.getIndex()){
return RecordComparator.PRECEDES;
}else{
return RecordComparator.EQUIVALENT;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
在添加字段的时候,我还要查找该字段有没有存在,我到网上查了一下都说,把数据全部删了,再添加所有的数据这样比较快:
RMS的操作类如下:
public class RecordUtil {
private static RecordUtil instance;
private static RecordStore rs;
private RecordEnumeration re;
private Vector vector;
private static ShowQuotesCategory sqc;
/**
* 实例化RecordStore对象,并在rs中存入几个值
* @param storeName
* @return
*/
public static RecordUtil getInstance(String storeName){
if(instance==null){
try {
rs=RecordStore.openRecordStore(storeName, true);
sqc=new ShowQuotesCategory();
//添加几个默认的值
if(rs.getNumRecords()<=0){
byte[] b;
b=Resources.CATEGORY_QUOTEST_LIST_ZUIXIN.object2ByteArray();
rs.addRecord(b, 0, b.length);
b=Resources.CATEGORY_QUOTEST_LIST_ZHANGFU.object2ByteArray();
rs.addRecord(b, 0, b.length);
b=Resources.CATEGORY_QUOTEST_LIST_ZHANGDIE.object2ByteArray();
rs.addRecord(b, 0, b.length);
}
} catch (Exception e) {
e.printStackTrace();
}
instance=new RecordUtil();
}
return instance;
}
public String getStortName(){
if(rs!=null){
try {
return rs.getName();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
}
}
return null;
}
public Vector getRecord(RecordFilter rf,RecordComparator rc,boolean flag){
if(rs!=null){
vector=new Vector();
try {
re=rs.enumerateRecords(rf, rc, flag);
while(re.hasNextElement()){
sqc=ShowQuotesCategory.byteArray2Object(re.nextRecord());
vector.addElement(sqc);
}
return vector;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 添加数据到rms,在执行这一步的时候一定要先删除掉这个结果集
*
*/
public Vector addRecord(Vector v,RecordFilter rf,RecordComparator rc,boolean flag){
if(rs!=null){
try {
String name=getStortName();
closeRecord();
RecordStore.deleteRecordStore(name);
rs=RecordStore.openRecordStore(name, true);
//添加几个默认的值
if(rs.getNumRecords()<=0){
byte[] b;
b=Resources.CATEGORY_QUOTEST_LIST_ZUIXIN.object2ByteArray();
rs.addRecord(b, 0, b.length);
b=Resources.CATEGORY_QUOTEST_LIST_ZHANGFU.object2ByteArray();
rs.addRecord(b, 0, b.length);
b=Resources.CATEGORY_QUOTEST_LIST_ZHANGDIE.object2ByteArray();
rs.addRecord(b, 0, b.length);
}
if(v.size()>0){
for (int i = 0; i < v.size(); i++) {
sqc=(ShowQuotesCategory)v.elementAt(i);
rs.addRecord(sqc.object2ByteArray(), 0, sqc.object2ByteArray().length);
}
}
return this.getRecord(rf, rc, flag);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public void closeRecord(){
if(rs!=null){
try {
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}