📄 mainselect.java
字号:
{
dos.writeBoolean(true);
RecordStore musicRecord = null;
musicRecord=RecordStore.openRecordStore("MusicRecord",true);
recordNumber = musicRecord.getNumRecords();
if(recordNumber == 0)
dos.writeInt(0);
else
{
dos.writeInt(recordNumber);
RecordEnumeration e = null;
byte[] data = null;
e = musicRecord.enumerateRecords(null, null, false);
while(e.hasNextElement())
{
data = e.nextRecord();
dos.writeInt(data.length);
dos.write(data, 0, data.length);
}
}
musicRecord.closeRecordStore();
}
else
dos.writeBoolean(false);
//System.out.println("开始备份记录索引");
if(selectArray[3])
{//备份记录索引
dos.writeBoolean(true);
RecordStore indexRecord = null;
indexRecord = RecordStore.openRecordStore("IndexRecord",true);
recordNumber = indexRecord.getNumRecords();
if(recordNumber == 0)
dos.writeInt(0);
else
{
dos.writeInt(recordNumber);
RecordEnumeration e = null;
byte[] data = null;
e = indexRecord.enumerateRecords(null, null, false);
while(e.hasNextElement())
{
data = e.nextRecord();
dos.writeInt(data.length);
dos.write(data, 0, data.length);
}
}
indexRecord.closeRecordStore();
//System.out.println("开始备份走步记录");
//备份走步记录
RecordStore contentRecord = null;
contentRecord = RecordStore.openRecordStore("ContentRecord",true);
recordNumber = contentRecord.getNumRecords();
//System.out.println("取得记录数:" + recordNumber);
dos.writeInt(recordNumber);
if(recordNumber != 0)
{
RecordEnumeration e = null;
int[] recordId = null;
e = contentRecord.enumerateRecords(null, null, false);
//System.out.println("取得走步记录的ID集合,数目是:" + e.numRecords());
int[] recordIds = new int[e.numRecords()];
int tempNumber = 0;
while(e.hasNextElement())
{
recordIds[tempNumber++] = e.nextRecordId();
}
//System.out.println("取得走步记录的ID集合完毕,开始排序");
/*for(int i = 0; i < recordIds.length; i++)
System.out.println(recordIds[i]);*/
sort(recordIds, 0, recordIds.length - 1);
//System.out.println("排序完成");
/*for(int i = 0; i < recordIds.length; i++)
System.out.println(recordIds[i]);*/
byte[] data = null;
for(int i = 0; i < recordIds.length; i++)
{
data = contentRecord.getRecord(recordIds[i]);
dos.writeInt(data.length);
dos.write(data, 0, data.length);
}
//System.out.println("走步记录的ID集合备份完成");
}
contentRecord.closeRecordStore();
}
else
dos.writeBoolean(false);
//System.out.println("所选择的备份完毕");
dos.close();
fc.close();
}
catch(IOException ie)
{
//System.out.println("出现IO异常");
Alert alert=new Alert("Message","备份过程中出现文件操作异常,请重新进行,若还不行,重新启动程序再备份一定行",null,AlertType.WARNING);
Chess.dis.setCurrent(alert, this);
flash.stopFlash();
return 1;
}
catch(RecordStoreException rse)
{
//System.out.println("出现存储异常");
Alert alert=new Alert("Message","备份过程中出现存储记录操作异常,请重新进行,若还不行,重新启动程序再备份一定行",null,AlertType.WARNING);
Chess.dis.setCurrent(alert, this);
flash.stopFlash();
return 2;
}
catch(Exception e)
{
//System.out.println("出现未知异常");
Alert alert=new Alert("Message","备份过程中出现未知记录操作异常,请重新进行,若还不行,重新启动程序再备份一定行",null,AlertType.WARNING);
Chess.dis.setCurrent(alert, this);
flash.stopFlash();
return 3;
}
Chess.dis.setCurrent(this);
flash.stopFlash();
return 0;
}
public void setSavePath(String savePath)
{//取得保存路径,开始选择备份项目
//System.out.println("取得保存路径,开始选择备份项目");
String[] tempString = {"皮肤风格", "残局", "背景音乐列表", "对战记录"};
this.savePath = savePath;
backupList = new List("选择需要备份的项", Choice.MULTIPLE, tempString, null);
for(int i = 0; i < 4; i++)
{
backupList.setSelectedIndex(i, true);
}
backupList.addCommand(enter);
backupList.addCommand(exit);
backupList.setCommandListener(this);
Chess.dis.setCurrent(backupList);
//System.out.println("项目选择构建完毕");
}
public int setRevertFile(String revertPath)
{
Flash flash = new Flash(this);
flash.reverting();
flash.startFlash();
FileConnection fc = null;
DataInputStream dis = null;
int recordNumber = 0;
boolean isRevert = false;
try
{
//System.out.println("开始打开文件");
//建立输入流
fc = (FileConnection)Connector.open(revertPath, Connector.READ);
//System.out.println("开始创建输入流");
dis = fc.openDataInputStream();
//还原皮肤风格
isRevert = dis.readBoolean();
//System.out.println("开始还原皮肤风格");
if(isRevert)
{
recordNumber = dis.readInt();
if(recordNumber != 0)
{
int tempLength = dis.readInt();
byte[] data = new byte[tempLength];
dis.read(data, 0, tempLength);
RecordStore setupRecord = RecordStore.openRecordStore("SetupRecord", true);
if(setupRecord.getNumRecords() == 0)
{
setupRecord.addRecord(data, 0, tempLength);
}
else
{
setupRecord.setRecord(1, data, 0, tempLength);
}
setupRecord.closeRecordStore();
}
}
//System.out.println("开始还原残局");
//还原残局
isRevert = dis.readBoolean();
if(isRevert)
{
recordNumber = dis.readInt();
if(recordNumber != 0)
{
RecordStore restRecord = RecordStore.openRecordStore("PhaseRecord", true);
for(int i = 0; i < recordNumber; i++)
{
int tempLength = dis.readInt();
byte[] data = new byte[tempLength];
dis.read(data, 0, tempLength);
restRecord.addRecord(data, 0, tempLength);
}
restRecord.closeRecordStore();
}
}
//System.out.println("开始还原音乐列表");
//还原音乐列表
isRevert = dis.readBoolean();
if(isRevert)
{
recordNumber = dis.readInt();
if(recordNumber != 0)
{
RecordStore musicRecord = RecordStore.openRecordStore("MusicRecord", true);
for(int i = 0; i < recordNumber; i++)
{
int tempLength = dis.readInt();
byte[] data = new byte[tempLength];
dis.read(data, 0, tempLength);
musicRecord.addRecord(data, 0, tempLength);
}
musicRecord.closeRecordStore();
}
}
//System.out.println("开始还原记录");
//还原记录索引
isRevert = dis.readBoolean();
if(isRevert)
{
recordNumber = dis.readInt();
if(recordNumber != 0)
{//有记录需要还原
//System.out.println("有记录需要还原:" + recordNumber);
//查看是否已经有记录,如果有,则删除
String[] recordStoreList = RecordStore.listRecordStores();
if(recordStoreList != null)
{
//System.out.println("已经存在一些记录");
for(int i = 0; i < recordStoreList.length; i++)
{
if(recordStoreList[i].equals("IndexRecord"))
{//已经存在走步记录
RecordStore.deleteRecordStore("IndexRecord");
}
if(recordStoreList[i].equals("ContentRecord"))
{//已经存在走步记录
RecordStore.deleteRecordStore("ContentRecord");
}
}
}
//System.out.println("删除已经存在的记录");
}
else
{//无记录需要还原
return 0;
}
//System.out.println("开始还原记录索引");
//还原IndexRecord
RecordStore indexRecord = RecordStore.openRecordStore("IndexRecord", true);
for(int i = 0; i < recordNumber; i++)
{
int tempLength = dis.readInt();
byte[] data = new byte[tempLength];
dis.read(data, 0, tempLength);
indexRecord.addRecord(data, 0, tempLength);
}
indexRecord.closeRecordStore();
//System.out.println("记录索引还原完毕开始还原走步记录");
recordNumber = dis.readInt();
//System.out.println("走步记录个数为:" + recordNumber);
if(recordNumber != 0)
{
RecordStore contentRecord = RecordStore.openRecordStore("ContentRecord", true);
for(int i = 0; i < recordNumber; i++)
{
int tempLength = dis.readInt();
byte[] data = new byte[tempLength];
dis.read(data, 0, tempLength);
contentRecord.addRecord(data, 0, tempLength);
}
contentRecord.closeRecordStore();
}
//System.out.println("走步记录还原完毕");
}
dis.close();
fc.close();
Chess.dis.setCurrent(this);
flash.stopFlash();
}
catch(IOException ie)
{
Alert alert=new Alert("Message","还原过程中出现文件操作异常,请重新进行,若还不行,重新启动程序再备份一定行",null,AlertType.WARNING);
Chess.dis.setCurrent(alert, this);
flash.stopFlash();
return 1;
}
catch(RecordStoreException rse)
{
Alert alert=new Alert("Message","还原过程中出现记录操作异常,请重新进行,若还不行,重新启动程序再备份一定行",null,AlertType.WARNING);
Chess.dis.setCurrent(alert, this);
flash.stopFlash();
return 2;
}
catch(Exception e)
{
Alert alert=new Alert("Message","还原过程中出现未知异常,请重新进行,若还不行,重新启动程序再备份一定行",null,AlertType.WARNING);
Chess.dis.setCurrent(alert, this);
flash.stopFlash();
return 3;
}
return 0;
}
//堆排序算法
private int partition(int[] array, int low, int high)
{
int key = array[low];
while(low < high)
{
while(low < high && array[high] >= key)
high--;
array[low] = array[high];
while(low < high && array[low] <= key)
low++;
array[high] = array[low];
}
array[low] = key;
return low;
}
private void sort(int[] array, int low, int high)
{
if(low < high)
{
int midInt = partition(array, low, high);
sort(array, low, midInt - 1);
sort(array, midInt + 1, high);
}
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -