📄 scoredata.java
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.rms.RecordStore;
public class ScoreData {
private RecordStore rs;
private int id; //记录ID
private String rsName; //记录仓库名
private byte[] tmpData; //临时字节数组
Image competImg;
//数据流
private ByteArrayInputStream bais;
private DataInputStream dis;
private ByteArrayOutputStream baos;
private DataOutputStream dos;
public ScoreData() {
rsName = "elceHitDiamond";
/*int [] tmp0={10,30,40,50,0,20,0,20,20,20,
0,20,20,0,20,20,20,20,20,0};
boolean [] tmp1={false,false,true,false,false,false,false,false,false,false,
false,false,false,false,true,false,false,false,false,false};
boolean [] tmp2={false,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true};
writeDate(0, tmp0, tmp1, tmp2);
writeDate(1, tmp0, tmp1, tmp2);
writeDate(2, tmp0, tmp1, tmp2);
writeDate(3, tmp0, tmp1, tmp2);
writeDate(4, tmp0, tmp1, tmp2);
writeDate(5, tmp0, tmp1, tmp2);
writeDate(6, tmp0, tmp1, tmp2);
writeDate(7, tmp0, tmp1, tmp2);*/
//deleteRS();
}
/**数据流**/
//*======输入======*//
protected void readStream(byte[] data) {
bais = new ByteArrayInputStream(data);
dis = new DataInputStream(bais);
}
//*======输出======*//
protected void writeStream() {
baos = new ByteArrayOutputStream();
dos = new DataOutputStream(baos);
}
/**主流程**/
//(1)======是否有记录======*//
// get level
protected int getLevel() {
rs = openRS();
int tmpLevel = 0;
try {
tmpLevel = rs.getNextRecordID()-1;
rs.closeRecordStore();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("RecordStore get error!!");
}
return tmpLevel;
}
//(2)======写数据======*//
protected void writeDate(int level, int[] score, boolean[] stars, boolean[] subPass) {
int tmpLevel = level + 1;
rs = openRS();
try {
int endId = rs.getNextRecordID();
if (endId == 1 || tmpLevel==endId) { //没有记录
byte[] result = write(score, stars, subPass); //write方法
rs.addRecord(result, 0, result.length); //添加
} else { //有记录
byte[] result = write(score, stars, subPass); //write方法
rs.setRecord(tmpLevel, result, 0, result.length); //修改记录
}
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
System.out.println("writeDate error!!");
}
}
//(3)======显示排行======*//
protected int[] getAllScore() {
rs = openRS(); //打开
int [] score = new int[20];
int [] scoreCount=null;
int count=0;
try {
//容纳记录的数组
int endId = rs.getNextRecordID();
scoreCount = new int[endId-1];
for (int i = 1; i < endId; i++) {
byte[] result = rs.getRecord(i);
for(int j = 0; j < 20; j++){
count += getScore(result)[j];
}
scoreCount[i-1]=count; //统计
count=0;
//System.out.println("stars[i - 1]" + stars[i - 1]);
}
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
System.out.println("getStar error!!");
}
return scoreCount;
}
protected int[] getAllStars() {
rs = openRS(); //打开
boolean [] stars = new boolean[20];
int []starsCount=null;
int count=0;
try {
//容纳记录的数组
int endId = rs.getNextRecordID();
starsCount = new int[endId-1];
for (int i = 1; i < endId; i++) {
byte[] result = rs.getRecord(i);
for(int j = 0; j < 20; j++){
if(getStars(result)[j]) //有星星
count++;
}
starsCount[i-1]=count; //统计
count=0;
//System.out.println("stars[i - 1]" + stars[i - 1]);
}
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
System.out.println("getStar error!!");
}
return starsCount;
}
//分数
protected int[] getSubScore(int level) {
rs = openRS(); //打开
int []subScore = null;
try {
//容纳记录的数组
byte[] result = rs.getRecord(level+1);
subScore=getScore(result);
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
System.out.println("getSubPass error!!");
}
return subScore;
}
//小关星星
protected boolean[] getSubStars(int level) {
rs = openRS(); //打开
boolean []subStars = null;
try {
//容纳记录的数组
byte[] result = rs.getRecord(level+1);
subStars=getStars(result);
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
System.out.println("getSubPass error!!");
}
return subStars;
}
protected boolean[] getSubPass(int level) {
rs = openRS(); //打开
boolean []subPass = null;
try {
//容纳记录的数组
byte[] result = rs.getRecord(level+1);
subPass=subPass(result);
rs.closeRecordStore();
} catch (Exception e) {
e.printStackTrace();
System.out.println("getSubPass error!!");
}
return subPass;
}
/**记录操作方法**/
/* ======添加到BYTE数组====== */
/*public byte[] write(int score, int stars) {
writeStream();
byte[] result = null;
try {
dos.writeInt(score);
dos.writeInt(stars);
//每小关是否过
result = baos.toByteArray();
baos.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("RecordStore add error!!");
}
return result;
}*/
public byte[] write(int[] score, boolean[] stars, boolean[] subPass) {
writeStream();
byte[] result = null;
try {
for(int i=0; i<20; i++){
dos.writeInt(score[i]);
}
for(int i=0; i<20; i++){
dos.writeBoolean(stars[i]);
}
//每小关是否过
for(int i=0; i<20; i++){
dos.writeBoolean(subPass[i]);
}
result = baos.toByteArray();
baos.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("RecordStore add error!!");
}
return result;
}
/* ======取得数据====== */
private int[] getScore(byte[] data) {
int[] tmpScore =new int[20];
readStream(data);
try {
for(int i=0; i<20; i++){
tmpScore[i] = dis.readInt();
}
bais.close();
dis.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("getScore error!!");
}
return tmpScore;
}
private boolean [] getStars(byte[] data) {
boolean []stars=new boolean[20];
readStream(data);
try {
//分数
for(int i=0; i<20; i++){
dis.readInt();
}
for(int i=0; i<20; i++){
stars[i]=dis.readBoolean();
}
bais.close();
dis.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("getScore error!!");
}
return stars;
}
private boolean [] subPass(byte[] data) {
boolean []subPass=new boolean[20];
readStream(data);
try {
//分数
for(int i=0; i<20; i++){
dis.readInt();
}
//星星
for(int i=0; i<20; i++){
dis.readBoolean();
}
//每小关
for(int i=0; i<20; i++){
subPass[i]=dis.readBoolean();
}
bais.close();
dis.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("subPass error!!");
}
return subPass;
}
/* ======打开记录仓库====== */
protected RecordStore openRS() {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(rsName, true);
return rs;
} catch (Exception e) {
System.out.println("RecordStore open error!!");
return null;
}
}
/* ======删除记录仓库====== */
protected void deleteRS() {
//删除
try {
RecordStore.deleteRecordStore(rsName);
} catch (Exception e) {
e.printStackTrace();
System.out.println("RecordStore delete error!!");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -