首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

怎么存取数据

2012-01-13 
如何存取数据?用户输入的数据需要保存,请问用什么函数可以实现,谢谢刚接触手机编程,问题很肤浅,见笑了[解

如何存取数据?
用户输入的数据需要保存,请问用什么函数可以实现,谢谢

刚接触手机编程,问题很肤浅,见笑了

[解决办法]
要看你保存在什么地方,你可以保存到文件,也可以保存到RMS,看你设计而定,而且,没有一个方法就能实现的
[解决办法]
能举个例子么?谢谢
[解决办法]
Package javax.microedition.rms

用于进行存储的包,负责数据的存取
[解决办法]
class RMS {
static String missionRMS = "MISSION_RMS ";
public RMS() {

}
void storeTask(Task task) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
// write task info
dos.writeInt(task.id);
dos.writeUTF(task.label);
dos.writeUTF(task.description);
dos.writeUTF(task.rewardInfo);
dos.writeInt(task.price);
// write components info
TaskComponent[] taskComponents = task.taskComponents;
int numTaskComponents = taskComponents.length;
dos.writeInt(numTaskComponents);
for (int i = 0; i < numTaskComponents; i++) {
TaskComponent component = taskComponents[i];
NPC npc = component.npc;
// write npc info in this component
dos.writeInt(npc.id);
dos.writeInt(npc.countryID);
dos.writeInt(npc.sex);
dos.writeInt(npc.ai);
dos.writeInt(npc.level);
dos.writeUTF(npc.name);
// component info
dos.writeInt(component.numWinTimes);
dos.writeInt(component.shouldWinSequentially);
}
byte data[] = baos.toByteArray();
RecordStore rs = openRecordStore();
if (rs != null) {
if (rs.getNumRecords() <= 0) {
rs.addRecord(data, 0, data.length);
System.out.println( "Adding new record ");
} else {
rs.setRecord(1, data, 0, data.length);
System.out.println( "updating record ");
}
rs.closeRecordStore();
System.out.println( "Task saved successfully. ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
Task getTask() {
try {
RecordStore rs = openRecordStore();
if (rs == null || rs.getNumRecords() <= 0) {
if (rs != null) {
rs.closeRecordStore();
}
return null;
} else {
byte[] data = rs.getRecord(1);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
int id, price, numComponents;
String label, description, rewardInfo;
TaskComponent[] taskComponents;
id = dis.readInt();
label = dis.readUTF();
description = dis.readUTF();
rewardInfo = dis.readUTF();
price = dis.readInt();
System.out.println( "id : " + id);
System.out.println( "label : " + label);
System.out.println( "description : " + description);
System.out.println( "rewardInfo " + rewardInfo);
// components
numComponents = dis.readInt();
taskComponents = new TaskComponent[numComponents];
for (int i = 0; i < numComponents; i++) {
int npcID, npcCountryID, npcSex, npcAI, npcLevel, numWinTimes, shouldWinSequentially;
String npcName;
npcID = dis.readInt();
npcCountryID = dis.readInt();
npcSex = dis.readInt();
npcAI = dis.readInt();
npcLevel = dis.readInt();
npcName = dis.readUTF();
numWinTimes = dis.readInt();


shouldWinSequentially = dis.readInt();
System.out.println( "npcID : " + npcID);
System.out.println( "npcCountryID : " + npcCountryID);
System.out.println( "npcSex : " + npcSex);
System.out.println( "npcAI : " + npcAI);
System.out.println( "npcLevel : " + npcLevel);
System.out.println( "npcName : " + npcName);
System.out.println( "numWinTimes : " + numWinTimes);
System.out.println( "shouldWinSequentially : " + shouldWinSequentially);
taskComponents[i] = new TaskComponent(new NPC(npcID, npcName, npcCountryID, npcSex, npcAI, npcLevel, null), numWinTimes, shouldWinSequentially);
}
rs.closeRecordStore();
return new Task(0, id, price, label, description, rewardInfo, taskComponents);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
void removeTask() {
try {
RecordStore.deleteRecordStore(missionRMS);
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
RecordStore openRecordStore() {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(missionRMS, true);
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
return rs;
}

}
[解决办法]
import javax.microedition.rms
int i;//record index
byte[] data;//record data
int offset;//record start bit position
int length;//record length (bit)
init data(){
//init data;
}
length = data.length();
RecordStore rs = RecordStore.setRecord(1, data, offset, length); //add a new record
[解决办法]
RMS占用的内存空间,如何存储空间过大,应该会影响手机性能。不过,RMS能够占用的空间和手机有关。有些手机有限制,如S40。有些则没有限制,只和手机的存储空间相关。
[解决办法]
要是怕数据会太多,不如考虑将数据传输到服务器上

热点排行