import javax microedition rms import java io public class ResourceMana

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import javax.microedition.rms.*;
import java.io.*;
public class ResourceManager {
protected final String __DB_NAME__ = "test";
protected RecordStore db = null;
public boolean writeString(String str) {
try {
db = RecordStore.openRecordStore(__DB_NAME__, true);
}catch(RecordStoreException rse) {
rse.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try{
dos.writeUTF(str);
}catch(IOException ioe) {
ioe.printStackTrace();
}
byte[] record = baos.toByteArray();
try{
db.addRecord(record, 0, record.length);
return true;
}catch(RecordStoreException rse) {
rse.printStackTrace();
}
return false;
}
public String readString(int id) {
try {
db = RecordStore.openRecordStore(__DB_NAME__, false);
}catch(RecordStoreException rse) {
rse.printStackTrace();
}
String stringData = "";
try {
byte[] record = db.getRecord(1);
ByteArrayInputStream bais = new ByteArrayInputStream(record);
DataInputStream dis = new DataInputStream(bais);
stringData = dis.readUTF();
}catch(IOException ioe) {
ioe.printStackTrace();
}catch(RecordStoreException rse) {
rse.printStackTrace();
}
return stringData;
}
}