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

📄 rmsmobile.java

📁 J2ME飞机设计游戏,希望对学习J2ME的程序员有所帮助
💻 JAVA
字号:
import java.util.Hashtable;

import javax.microedition.rms.*;

public class RmsMobile {
	private RmsMobile(){
	}
	
	public static RmsMobile getInstance() {
		if (rms == null) {
			rms = new RmsMobile();
		}

		return rms;
	}

	public void openRecordStore(){
		try{
			recordStore = RecordStore.openRecordStore(rsName, true);
		}catch(Exception e){
			System.out.println("open exception!");
			e.printStackTrace();
		}
	}

	public void closeRecordStore() {
		try {
			recordStore.closeRecordStore();
			recordStore = null;
		} catch (Exception e) {
			System.out.println("close exception!");
			e.printStackTrace();
		}
	}

	public void saveRecordStore(String playerId, int iScore) {
		try {
			//System.out.println("num==" + recordStore.getNumRecords());
			if (recordStore.getNumRecords() > 0) {
				Hashtable hashtable = new Hashtable();
				loadRecordStore(); // 读取RMS里面的数据
				
				//System.out.println("playerScore.length==" + playerScore.length);
				
				int[] tmpScore = new int[playerScore.length + 1];

				for (int idx = 0; idx < playerScore.length; idx++) {
					tmpScore[idx] = Integer.parseInt(playerScore[idx]);

					System.out.println("playerScore[" + idx + "]===" + playerScore[idx]);
					System.out.println("playerName[" + idx + "]====" + playerName[idx]);
					
					hashtable.put(playerScore[idx], playerName[idx]);
				}

				tmpScore[recordStore.getNumRecords()] = iScore;
				hashtable.put(String.valueOf(iScore), playerId);

				//插入排序
				insert(tmpScore, tmpScore.length - 1,
						tmpScore[tmpScore.length - 1]);

				// 更新RMS
				System.out.println("tmpScore.length===" + tmpScore.length);
				for (int i = 0; i < tmpScore.length - 1; i++) {
					
					String info = hashtable.get(String.valueOf(tmpScore[i]))
							+ "/" + tmpScore[i];
					//System.out.println("info==" + info);
					
					recordStore.setRecord(i + 1, info.getBytes(), 0, info.getBytes().length);
					info = null;
				}

				//只保存10条记录
				if(tmpScore.length < 9){
					//	添加一条记录
					//System.out.println("save===" + scoreInfo[tmpScore.length - 1].toString());
					String lstInfo = hashtable.get(String
							.valueOf(tmpScore[tmpScore.length - 1]))
							+ "/" + tmpScore[tmpScore.length - 1];
					System.out.println("lstInfo===" + lstInfo);
					
					save(lstInfo);
					lstInfo = null;
				}

				hashtable = null;
			}else{
				save(playerId + "/" + iScore);
			}
		} catch (Exception e) {
			System.out.println("save!!!!!!!!");
			e.printStackTrace();
		}
	}
	
	private void save(String info) {
		try {
			byte[] data = info.getBytes();
			recordStore.addRecord(data, 0, data.length);
		} catch (Exception e) {
			System.out.println("save!!!!!!!!");
		}
	}

	public void loadRecordStore() {
		try {
			byte count = 0;
			//System.out.println("NumRecords==" + recordStore.getNumRecords());
			playerName = new String[recordStore.getNumRecords()];
			playerScore = new String[recordStore.getNumRecords()];
			for (int i = 1; i < recordStore.getNumRecords() + 1; i++) {
					byte nameEndIdx = 0;
					
					byte[] data = recordStore.getRecord(i);
					String strInfo = new String(data);
					//System.out.println("strInfo===" + strInfo);
					
					nameEndIdx = (byte) strInfo.indexOf("/", 0);
					//System.out.println("playerName==" + strInfo.substring(0, nameEndIdx));
					playerName[count] = strInfo.substring(0, nameEndIdx);
					
					
					//System.out.println("playerScore==" + strInfo.substring(nameEndIdx + 1, strInfo
					//		.length()));
					playerScore[count++] = strInfo.substring(nameEndIdx + 1, strInfo
							.length());
			}
		} catch (Exception e) {
			System.out.println("22222222");
			e.printStackTrace();
		}
	}

	/**
	 * 在已经排序好的数组中插入一个元素,使插入后的数组仍然有序
	 * 
	 * @param num  已经排序好的数组
	 * @param size 已经排序好的数组的大小
	 * @param c    待插入的元素
	 */
     public void insert(int[] num, int size, int c) {
		for (int i = 0; i < size; i++) {
			if (num[i] < c) {
				//System.out.println(num[i]);
				// 如果待插入的元素小于当前元素,则把当前元素后面的元素依次后移一位
				for (int j = size; j > i; j--) {
					num[j] = num[j - 1];
				}
				num[i] = c;
				break;
			}
		}
	}

    public String[] getPlayerName() {
 		return playerName;
 	}

 	public String[] getPlayerScore() {
 		return playerScore;
 	}

	private static String rsName = "miller007";
	private RecordStore recordStore;  //分数存储
	private String [] playerName = null;   //分数信息
	private String [] playerScore = null;     //玩家姓名
	private static RmsMobile rms;
}

⌨️ 快捷键说明

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