首页 >> 民生风情

简单封装MIDPRMS操作节能

民生风情  2020-11-07 04:24 字号: 大 中 小

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

package s;

import .*;

import s.*;

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));

ad(input);

return true;

}

catch(IOException ioe) {

return false;

}

finally {

try {

ose();

}

catch (Exception e) {}

}

}

public static boolean saveUniqueRecord(Persistentable p) {

DataOutputStream output = null;

try {

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

output = new DataOutputStream(bytes);

ve(output);

return saveUniqueRecord(ByteArray());

}但局面很快会得到控制。

catch(IOException ioe) {

return false;

}

finally {

if(output!=null)

try {

ose();

}

catch (Exception e) {}

}

}

private static byte[] loadUniqueRecord() {

RecordStore rs = null;

RecordEnumeration re = null;

try {

rs = enRecordStore(RECORD_STORE_NAME, true);

if(tNumRecords()==0)

return null;

re = umerateRecords(null, null, false);

if(sNextElement())

return xtRecord();

return null;

}

catch(RecordStoreException rse) {

return null;

}

finally {

if(re!=null) {

stroy();

}

if(rs!=null) {

try {

oseRecordStore();

}

catch (Exception e) {}

}

}

}

private static boolean saveUniqueRecord(byte[] data) {

RecordStore rs = null;

RecordEnumeration re = null;

try {

rs = enRecordStore(RECORD_STORE_NAME, true);

re = umerateRecords(null, null, false);

if(sNextElement()) {

tRecord(xtRecordId(), data, 0, ngth);

}

else {

dRecord(data, 0, ngth);

}

return true;

}

catch(RecordStoreException rse) {

return false;

}

finally {

if(re!=null) {

stroy();

}

if(rs!=null) {

try {

oseRecordStore();

}

catch (Exception e) {}

}

}

}

}

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

package s;

import .*;

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");

loa平台拥有着国内最精品的页游戏d(this);

}

public void save(DataOutputStream output) throws IOException {

iteInt(score);

iteUTF8(username);

}

public void load(DataInputStream input) throws IOException {

score = adInt();

username = adUTF8();

}

}

查看本文来源

灰指甲清除病菌
灰指甲用亮甲效果怎样
三亚牛皮癣医院有哪些
推荐资讯