📄 save.java
字号:
///Save.java save record such as high score,game level
package Speeds;
import javax.microedition.rms.*;
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
public class Save
{
protected RecordStore recordStore=null;
protected RecordEnumeration re=null;
public Save() {
try
{
recordStore=RecordStore.openRecordStore("save",true);
re=recordStore.enumerateRecords(null,null,true);
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
}
public int getId(String vName) {
int ret=0;
try {
re.reset();
while(re.hasNextElement()) {
int id = re.nextRecordId();
ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id));
DataInputStream inputStream = new DataInputStream(bais);
try {
int value = inputStream.readInt();
String var = inputStream.readUTF();
if(var.equals(vName)) {
ret=id;
break;
}
}
catch (EOFException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
}
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
return ret;
}
public boolean checkSave() {
boolean ifSave=false;
int count=0;
try {
re.reset();
while(re.hasNextElement()) {
int id = re.nextRecordId();
count++;
}
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
if(count>0) {
//System.out.println("found record:"+count);
ifSave=true;
}
return ifSave;
}
public boolean updateRec(int value,String vName) {
int ret=0;
/////////////////get record id
try {
//int num=re.numRecords();
//System.out.println("total record:"+num);
re.reset();
while(re.hasNextElement()) {
int id = re.nextRecordId();
//System.out.println("come here");
ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id));
DataInputStream inputStream = new DataInputStream(bais);
try {
int tmp=inputStream.readInt();
String var = inputStream.readUTF();
//System.out.println("get var:"+var);
if(var.equals(vName)) {
ret=id;
//System.out.println("get id:"+ret);
break;
} else {
ret=1;
}
}
catch (EOFException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
}
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
//////////////////////////update record
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baos);
try {
outputStream.writeInt(value);
outputStream.writeUTF(vName);
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
// Extract the byte array
byte[] b = baos.toByteArray();
// Add it to the record store
try {
recordStore.setRecord(ret,b, 0, b.length);
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
return false;
}
return true;
}
public boolean saveProcess(int value,String varName) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baos);
try {
outputStream.writeInt(value);
outputStream.writeUTF(varName);
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
byte[] b = baos.toByteArray();
try {
recordStore.addRecord(b, 0, b.length);
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
return false;
}
return true;
}
public int readProcess(String varName) {
int ret=0;
int value=0;
String var=null;
try {
re.reset();
while(re.hasNextElement()) {
int id = re.nextRecordId();
ByteArrayInputStream bais = new ByteArrayInputStream(recordStore.getRecord(id));
DataInputStream inputStream = new DataInputStream(bais);
try {
value = inputStream.readInt();
var = inputStream.readUTF();
if(var.equals(varName)) {
ret=value;
//System.out.println("read var:"+varName+":"+ret);
break;
}
}
catch (EOFException eofe) {
System.out.println(eofe);
eofe.printStackTrace();
}
}
}
catch (RecordStoreException rse) {
System.out.println(rse);
rse.printStackTrace();
}
catch (IOException ioe) {
System.out.println(ioe);
ioe.printStackTrace();
}
return ret;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -