⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 restrecord.java

📁 这是一个Java编写的手机象棋游戏
💻 JAVA
字号:
//这个类中对与记录打开关闭存取等有关的异常,最好的处理办法是将其向上抛到调用它的地方,可以在那里进行处理,比如中止程序的继续运行,因为再运行下去肯定又会出现NullPointerException异常。
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.rms.*;

public class RestRecord
{
	private	RecordStore			phaseRecord=null;
	private RecordEnumeration	phaseEnum=null;
	private String[]			phaseNames=null;
	private int[]				phaseIds=null;
	
	public RestRecord()
	{
		openPhaseRecord();
	}
	private boolean openPhaseRecord()
	{
		try
		{
			phaseRecord=RecordStore.openRecordStore("PhaseRecord",true);
		}
		catch(RecordStoreException e)
		{
			//System.out.println("打开PhaseRecord出现异常");
			//errorDeal.push("RestRecord#1", "打开残局记录存储文件时出现错误");
			Alert alert=new Alert("Message","打开残局记录存储文件时出现错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
			return false;
		}
		return true;
	}
	public int closePhaseRecord()
	{
		try
		{
			phaseRecord.closeRecordStore();
		}
		catch(RecordStoreException e)
		{
			//System.out.println("关闭PhaseRecord时出现异常");
			//errorDeal.push("RestRecord#2", "关闭残局记录存储文件时出现错误");
			return 1;
		}
		return 0;
	}
	
	public	int[][]	getPhase(String	phaseName)
	{
		openPhaseRecord();
		int phaseId=0;
		//System.out.println("phaseNames的长度是:"+phaseNames.length);
		for(int i=0;i<phaseNames.length;i++)
		{
			if(phaseName.equals(phaseNames[i]))
			{
				phaseId=phaseIds[i];
				//System.out.println("循环中得到的phaseId是:"+phaseId);
				break;
			}
		}
		//System.out.println("得到的indexId是:"+phaseId);
		Phase	phase = new Phase();
		try
		{
			phase=phaseDeserialize(phaseRecord.getRecord(phaseId));
		}
		catch(RecordStoreNotOpenException e)
		{
			//System.out.println("获取残局时出现RecordStoreNotOpenException异常"+e);
			//errorDeal.push("RestRecord#3", "获取一个残局记录出现存储文件打开错误");
			Alert alert=new Alert("Message","获取一个残局记录出现存储文件打开错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		catch(InvalidRecordIDException e)
		{
			//System.out.println("获取残局出现InvalidRecordIDException异常"+e);
			//errorDeal.push("RestRecord#4", "获取一个残局记录出现非法ID错误");
			Alert alert=new Alert("Message","获取一个残局记录出现非法ID错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		catch(RecordStoreException e)
		{
			//System.out.println("获取残局时出现RecordStoreException异常"+e);
			//errorDeal.push("RestRecord#5", "获取一个残局记录出现未知的存储文件错误");
			Alert alert=new Alert("Message","获取一个残局记录出现未知的存储文件错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		closePhaseRecord();
		return phase.getPhase();
	}
	
	public String[]	getPhaseList()
	{
		openPhaseRecord();
		try
		{
			phaseEnum=phaseRecord.enumerateRecords(null,null,false);
			phaseNames=new String[phaseEnum.numRecords()];
			phaseIds=new int[phaseEnum.numRecords()];
			//System.out.println("得到index条数为:"+phaseEnum.numRecords());
		}
		catch(RecordStoreNotOpenException rsno)
		{
			//errorDeal.push("RestRecord#6", "获取残局记录列表时出现存储文件打开错误");
			Alert alert=new Alert("Message","获取残局记录列表时出现存储文件打开错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		catch (RecordStoreException rse)
		{
			//errorDeal.push("RestRecord#7", "获取残局记录列表时出现未知存储文件错误");
			Alert alert=new Alert("Message","获取残局记录列表时出现未知存储文件错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		byte[] data=null;
		int i=0;
		try
		{
			while(phaseEnum.hasNextElement())
			{
				data=phaseEnum.nextRecord();
				Phase	phase=phaseDeserialize(data);
				phaseNames[i]=phase.getPhaseName();
				i=i+1;
				//System.out.println("得到一个phase名字:"+phase.getPhaseName());
			}
			int j=0;
			phaseEnum.reset();
			//索引恢复至刚刚建立之时
			while(phaseEnum.hasNextElement())
			{
				//System.out.println("进入遍历循环");
				phaseIds[j]=phaseEnum.nextRecordId();
				j=j+1;
			}
		}
		catch (RecordStoreException e)
		{
			//System.out.println("出现RecordStoreException异常");
			//errorDeal.push("RestRecord#8", "获取残局记录的名字的ID列表时出现未知存储文件错误");
			Alert alert=new Alert("Message","获取残局记录的名字的ID列表时出现未知存储文件错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		catch(Exception e)
		{
			//System.out.println("出现Exception异常"+e);
			//errorDeal.push("RestRecord#9", "获取残局记录列表时出现与存储文件操作无关的未知错误");
			Alert alert=new Alert("Message","获取残局记录列表时出现与存储文件操作无关的未知错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		closePhaseRecord();
		return phaseNames;
	}
	public int delPhase(String name)
	{
		openPhaseRecord();
		int phaseId=0;
		//System.out.println("phaseNames的长度是:"+phaseNames.length);
		for(int i=0;i<phaseNames.length;i++)
		{
			if(name.equals(phaseNames[i]))
			{
				phaseId=phaseIds[i];
				//System.out.println("循环中得到的phaseId是:"+phaseId);
				break;
			}
		}
		//System.out.println("得到的indexId是:"+phaseId);
		try
		{
			phaseRecord.deleteRecord(phaseId);
		}
		catch(RecordStoreNotOpenException e)
		{
			//System.out.println("删除残局时出现RecordStoreNotOpenException异常"+e);
			//errorDeal.push("RestRecord#10", "删除一条残局记录列表时出现存储文件打开错误");
			return 2;
		}
		catch(InvalidRecordIDException e)
		{
			//System.out.println("删除残局时出现InvalidRecordIDException异常"+e);
			//errorDeal.push("RestRecord#11", "删除一条残局记录列表时出现非法ID错误");
			return 3;
		}
		catch(RecordStoreException e)
		{
			//System.out.println("删除残局时出现RecordStoreException异常"+e);
			//errorDeal.push("RestRecord#12", "获取一条残局记录列表时出现未知存储文件错误");
			return 4;
		}
		closePhaseRecord();
		return 0;
	}
	public int	delAllPhase()
	{
		int flag = closePhaseRecord();
		if(flag==0)
		{
			try
			{
				RecordStore.deleteRecordStore("PhaseRecord");
				//System.out.println("记录全部删除成功");
			}
			catch(RecordStoreNotFoundException e)
			{
				//errorDeal.push("RestRecord#13", "删除所有残局记录时没有找到存储文件");
				closePhaseRecord();
				return 2;
			}
			catch(RecordStoreException e)
			{
				//errorDeal.push("RestRecord#14", "删除所有残局记录时出现未知存储文件错误");
				closePhaseRecord();
				return 3;
			}
		}
		else
		{
			//System.out.println("Flag的值是:"+flag);
			return flag;//此处flag值为1
		}
		openPhaseRecord();
		return 0;
	}
	public int delTail()
	{
		openPhaseRecord();
		int lastId=0;
		try
		{
			//System.out.println("开始获取最后一个phaseRecord的Id");
			lastId=phaseRecord.getNextRecordID()-1;
			//System.out.println("获取最后一个phaseRecord的Id是:"+lastId);
			phaseRecord.deleteRecord(lastId);
		}
		catch(RecordStoreNotOpenException e)
		{
			//System.out.println("删除最后一个phaseRecord时出现RecordStoreNotOpenException异常");
			//errorDeal.push("RestRecord#15", "删除最后一条残局记录时打开存储文件出现错误");
			return 1;
		}
		catch(InvalidRecordIDException e)
		{
			//System.out.println("删除最后一个phaseRecord时出现InvalidRecordIDException异常");
			//errorDeal.push("RestRecord#16", "删除所有残局记录时出现非法ID错误");
			return 2;
		}
		catch(RecordStoreException e)
		{
			//System.out.println("删除最后一个phaseRecord时出现RecordStoreException异常");
			//errorDeal.push("RestRecord#17", "删除所有残局记录时出现未知存储文件操作错误");
			return 3;
		}
		//System.out.println("删除最后一个phaseRecord成功完成");
		closePhaseRecord();
		return 0;
	}
//存在的问题——重命名只能重命名一次,要想第二次命名必须重新进入
	public int	renamePhase(String name,String newName)
	{
		openPhaseRecord();
		int phaseId=0;
		int tempPhaseNameId = 0;
		//System.out.println("phaseNames的长度是:"+phaseNames.length);
		for(int i=0;i<phaseNames.length;i++)
		{
			if(name.equals(phaseNames[i]))
			{
				phaseId=phaseIds[i];
				tempPhaseNameId = i;
				//System.out.println("循环中得到的phaseId是:"+phaseId);
				break;
			}
		}
		//System.out.println("得到的phaseId是:"+phaseId);
		byte[] data=null;
		Phase	newPhase = null;
		try
		{
			data=phaseRecord.getRecord(phaseId);
			newPhase=phaseDeserialize(data);
			newPhase.setPhaseName(newName);
			data=phaseSerialize(newPhase);
			phaseRecord.setRecord(phaseId,data,0,data.length);
			phaseNames[tempPhaseNameId] = newName;
		}
		catch(RecordStoreNotOpenException e)
		{
			//System.out.println("更新残局时出现RecordStoreNotOpenException异常"+e);
			//errorDeal.push("RestRecord#18", "重命名残局记录时打开存储文件错误");
			closePhaseRecord();
			return 2;
		}
		catch(InvalidRecordIDException e)
		{
			//System.out.println("更新残局时出现InvalidRecordIDException异常"+e);
			//errorDeal.push("RestRecord#19", "重命名残局记录时出现非法ID错误");
			closePhaseRecord();
			return 3;
		}
		catch(RecordStoreException e)
		{
			//System.out.println("更新残局时出现RecordStoreException异常"+e);
			//errorDeal.push("RestRecord#20", "重命名残局记录时出现未知存储文件操作错误");
			closePhaseRecord();
			return 4;
		}
		closePhaseRecord();
		return 0;
	}

	//***********************添加残局部分
	public int	addPhase(int[][] chess)
	{
		openPhaseRecord();
		//System.out.println("开始添加残局");
		Phase	phase=new Phase();
		//System.out.println(1);
		phase.setPhase(chess);
		//System.out.println(2);
		phase.setPhaseName();
		//System.out.println(3);
		byte[]	data=null;
		data=phaseSerialize(phase);
		//System.out.println("生成残局记录");
		try
		{
			phaseRecord.addRecord(data,0,data.length);
			//System.out.println("添加残局成功");
		}
		catch(RecordStoreException e)
		{
			//System.out.println("添加残局时出现异常"+e);
			//errorDeal.push("RestRecord#21", "添加残局记录时出现存储文件错误");
			closePhaseRecord();
			return 1;
		}
		closePhaseRecord();
		return 0;
	}
	//***********************序列化与反序列化部分
	private byte[]	phaseSerialize(Phase phase)
	{
		DataOutputStream		dos=null;
		ByteArrayOutputStream	baos=null;
		try
		{
			baos=new ByteArrayOutputStream();
			dos=new DataOutputStream(baos);
			//System.out.println("开始序列化");
			dos.writeUTF(phase.getPhaseName());
			//System.out.println("取得名字:"+phase.getPhaseName());
			int[][]	chess=phase.getPhase();
			for(int i=0;i<10;i++)
			{
				for(int j=0;j<9;j++)
				{
					dos.writeInt(chess[i][j]);
					//System.out.println(chess[i][j]);
				}
			}
			dos.close();
			baos.close();
		}
		catch (IOException e)
		{
			//System.out.println("序列化PhaseRecord时出现了异常");
			//errorDeal.push("RestRecord#22", "序列化残局时出现输入输出错误");
			Alert alert=new Alert("Message","序列化残局时出现输入输出错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		return baos.toByteArray();
	}
	private Phase	phaseDeserialize(byte[]	data)
	{
		ByteArrayInputStream  bais=null;
		DataInputStream		dis=null;
		int[][]	chess=new int[10][9];
		Phase	phase=new Phase();
		try
		{
			bais=new ByteArrayInputStream(data);
			dis=new DataInputStream(bais);
			phase.setPhaseName(dis.readUTF());
			for(int i=0;i<10;i++)
			{
				for(int j=0;j<9;j++)
				{
					chess[i][j]=dis.readInt();
				}
			}
			phase.setPhase(chess);
			dis.close();
			bais.close();
		}
		catch (IOException e)
		{
			//System.out.println("反序列化PhaseRecord时出现了异常");
			//errorDeal.push("RestRecord#23", "反序列化残局时出现输入输出错误");
			Alert alert=new Alert("Message","反序列化残局时出现输入输出错误",null,AlertType.WARNING);
			Chess.dis.setCurrent(alert);
			Chess.quitApp();
		}
		return phase;
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -