WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程简单封装MIDP RMS操作

简单封装MIDP RMS操作
作者:Xuefeng  来源:http://blog.csdn.net/asklxf  发布时间:2006-7-25 8:52:43

许多MIDP应用程序需要简单的存储配置信息,对此,简单封装一个RMSHandler,实现一条记录的读写:

package com.crackj2ee.midp.rms;
import java.io.*;
import javax.microedition.rms.*;
public final class RMSHandler {
    private static final String RECORD_STORE_NAME = "RmsDaTa";
    public static boolean loadUniqueRecord(Persistentable p) {
        byte[] data = loadUniqueRecord();
        if(data==null)
            return false;
        DataInputStream input = null;
        try {
            input = new DataInputStream(new ByteArrayInputStream(data));
            p.load(input);
            return true;
        }
        catch(IOException ioe) {
            return false;
        }
        finally {
            try {
                input.close();
            }
            catch (Exception e) {}
        }
    }
    public static boolean saveUniqueRecord(Persistentable p) {
        DataOutputStream output = null;
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            output = new DataOutputStream(bytes);
            p.save(output);
            return saveUniqueRecord(bytes.toByteArray());
        }
        catch(IOException ioe) {
            return false;
        }
        finally {
            if(output!=null)
                try {
                    output.close();
                }
                catch (Exception e) {}
        }
    }
    private static byte[] loadUniqueRecord() {
        RecordStore rs = null;
        RecordEnumeration re = null;
        try {
            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
            if(rs.getNumRecords()==0)
                return null;
            re = rs.enumerateRecords(null, null, false);
            if(re.hasNextElement())
                return re.nextRecord();
            return null;
        }
        catch(RecordStoreException rse) {
            return null;
        }
        finally {
            if(re!=null) {
                re.destroy();
            }
            if(rs!=null) {
                try {
                    rs.closeRecordStore();
                }
                catch (Exception e) {}
            }
        }
    }
    private static boolean saveUniqueRecord(byte[] data) {
        RecordStore rs = null;
        RecordEnumeration re = null;
        try {
            rs = RecordStore.openRecordStore(RECORD_STORE_NAME, true);
            re = rs.enumerateRecords(null, null, false);
            if(re.hasNextElement()) {
                rs.setRecord(re.nextRecordId(), data, 0, data.length);
            }
            else {
                rs.addRecord(data, 0, data.length);
            }
            return true;
        }
        catch(RecordStoreException rse) {
            return false;
        }
        finally {
            if(re!=null) {
                re.destroy();
            }
            if(rs!=null) {
                try {
                    rs.closeRecordStore();
                }
                catch (Exception e) {}
            }
        }
    }
}

需要持久化的类实现一个Persistentable接口:

package com.crackj2ee.midp.rms;
import java.io.*;
public interface Persistentable {
    void save(DataOutputStream output) throws IOException;
    void load(DataInputStream input) throws IOException;
}

读写数据时,按照顺序依次读写即可,例如:

class MyForm extends Form implements Persistentable {

    private int score;
    private String username;


    public MyForm() {
        super("Test");
        load(this);
    }



    public void save(DataOutputStream output) throws IOException {
        output.writeInt(score);
        output.writeUTF8(username);
    }
    public void load(DataInputStream input) throws IOException {
        score = input.readInt();
        username = input.readUTF8();
    }

}
[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码