使用很方便~
写入:打开~存存存存存~写~关~
读取:打开~读~关~取得取得取得取得~
最好自己写几个宏定义
比如我们的储存顺序是:主角的生命、金钱、当前时间
那么写:
public static final byte RMS_SAVE_LIST_ROLE_HP_SHORT = 0;// 主角的hp是以short类型来保存的
public static final byte RMS_SAVE_LIST_MONEY_INT = 1;// 金钱是以int类型保存
public static final byte RMS_SAVE_LIST_CURTIME_INT = 2;// 同上
在读取记录时就可以
CRMSManager.getShort(RMS_SAVE_LIST_ROLE_HP_SHORT);
基础类型接口----------------------------------------
声明了我们可以保存的所有数据类型 有需要可以自己添加新的数据类型并且自实现其add和read块
public interface CBaseData {
public static final byte
DATA_BYTE = 0,
DATA_SHORT = 1,
DATA_INT = 2,
DATA_CHAR = 3,
DATA_STRING = 4,
DATA_BOOLEAN = 5,
DATA_BYTE_ARRAY = 6,
DATA_SHORT_ARRAY = 7,
DATA_INT_ARRAY = 8,
DATA_CHAR_ARRAY = 9,
DATA_STRING_ARRAY = 10,
DATA_BOOLEAN_ARRAY = 11;
}
RMS数据库管理器----------------------------------------
其实就是个双链表 把我们的保存的数据类型也都按顺序记录下来了
*注意:禁止保存如add(8);这样的因为8默认是int类型所以会调用add(int args0)这个方法 但是有可能我们只想保存一个byte而已,所以为了严谨性,我们一律都要写成:byte temp = 8; add(temp); 这样显示的声明变量temp是一个byte,才能成功调用add(byte args0);
里面用到的CDataOutputStream、CDataInputStream、CByteVector、CVector 只是自己封装的几个通用类
可以用DataOutputStream、DataInputStream
CByteVector 只是一个存放比特数的容器
CVector 就是vector
使用方法
// the example of this class for user
public void saveGame(){
CRMSManager.openRMS(CRMSManager.RMS_NAME1, true);
// TODO: add game data to the recordMemoryStore /CRMSManager.add
CRMSManager.write();
CRMSManager.closeRMS();
CRMSManager.free();
}
public void readGame(){
CRMSManager.openRMS(CRMSManager.RMS_NAME1, false);
CRMSManager.read();
CRMSManager.closeRMS();
// TODO: set the elements from data(CVector) to the game data /CRMSManager.get
CRMSManager.free();
}
下面是源代码 可以按照上面给出的使用方法直接使用
public class CRMSManager implements CBaseData{
public static final
String RMS_NAME1 = "GAME_DATA";
private static
RecordStore rec = null;
private static
CDataOutputStream cdos = null;
private static
CDataInputStream cdis = null;
private static CByteVector dataList = null;
public static
CVector data = null;
public static void free(){
if(dataList != null){
dataList.removeAllElements();
dataList = null;
}
if(data != null){
data.removeAllElements();
data = null;
}
}
public static void openRMS(String name, boolean isWrite){
if(rec != null)
return;
try {
rec = RecordStore.openRecordStore(name, true);
if(isWrite)
cdos = new CDataOutputStream();
else
data = new CVector();
dataList = new CByteVector();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public static void read(){
try {
byte list[] = rec.getRecord(1);
byte tmp[] = rec.getRecord(2);
cdis = new CDataInputStream(tmp);
for(int i = 0; i < list.length; ++ i){
dataList.addElement(list[i]);
switch(list[i]){
case DATA_BOOLEAN:
data.addElement(readBoolean());
break;
case DATA_BOOLEAN_ARRAY:
data.addElement(readBooleanArray());
break;
case DATA_CHAR:
data.addElement(readChar());
break;
case DATA_CHAR_ARRAY:
data.addElement(readCharArray());
break;
case DATA_INT:
data.addElement(readInt());
break;
case DATA_BYTE:
data.addElement(readByte());
break;
case DATA_SHORT:
data.addElement(readShort());
break;
case DATA_INT_ARRAY:
data.addElement(readIntArray());
break;
case DATA_BYTE_ARRAY:
data.addElement(readByteArray());
break;
case DATA_SHORT_ARRAY:
data.addElement(readShortArray());
break;
case DATA_STRING:
data.addElement(readString());
break;
case DATA_STRING_ARRAY:
data.addElement(readStringArray());
break;
}
}
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(){
try {
cdos.flush();
byte [] tmp = CDataOutputStream.baos.toByteArray();
if(isEmpty()){
rec.addRecord(dataList.object, 0, dataList.object.length);
rec.addRecord(tmp, 0, tmp.length);
}else{
rec.setRecord(1, dataList.object, 0, dataList.object.length);
rec.setRecord(2, tmp, 0, tmp.length);
}
} catch (IOException e) {
e.printStackTrace();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public static void closeRMS(){
if(rec == null)
return;
try {
rec.closeRecordStore();
rec = null;
if(cdos != null)
cdos.free();
if(cdis != null)
cdis.free();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public static void add(int args0) throws IOException{
dataList.addElement(DATA_INT);
cdos.writeInt(args0);
}
public static void add(byte args0) throws IOException{
dataList.addElement(DATA_BYTE);
cdos.writeByte(args0);
}
public static void add(short args0) throws IOException{
dataList.addElement(DATA_SHORT);
cdos.writeShort(args0);
}
public static void add(boolean args0) throws IOException{
dataList.addElement(DATA_BOOLEAN);
cdos.writeBoolean(args0);
}
public static void add(char args0) throws IOException{
dataList.addElement(DATA_CHAR);
cdos.writeChar(args0);
}
public static void add(String args0) throws IOException{
dataList.addElement(DATA_STRING);
cdos.writeUTF(args0);
}
public static void add(int[] args0) throws IOException{
dataList.addElement(DATA_INT_ARRAY);
cdos.writeInt(args0.length);
for(int i = 0; i < args0.length; ++ i){
cdos.writeInt(args0[i]);
}
}
public static void add(byte[] args0) throws IOException{
dataList.addElement(DATA_BYTE_ARRAY);
cdos.writeInt(args0.length);
for(int i = 0; i < args0.length; ++ i){
cdos.writeByte(args0[i]);
}
}
public static void add(short[] args0) throws IOException{
dataList.addElement(DATA_SHORT_ARRAY);
cdos.writeInt(args0.length);
for(int i = 0; i < args0.length; ++ i){
cdos.writeShort(args0[i]);
}
}
public static void add(boolean[] args0) throws IOException{
dataList.addElement(DATA_BOOLEAN_ARRAY);
cdos.writeInt(args0.length);
for(int i = 0; i < args0.length; ++ i){
cdos.writeBoolean(args0[i]);
}
}
public static void add(char[] args0) throws IOException{
dataList.addElement(DATA_CHAR_ARRAY);
cdos.writeInt(args0.length);
for(int i = 0; i < args0.length; ++ i){
cdos.writeChar(args0[i]);
}
}
public static void add(String[] args0) throws IOException{
dataList.addElement(DATA_STRING_ARRAY);
cdos.writeInt(args0.length);
for(int i = 0; i < args0.length; ++ i){
cdos.writeUTF(args0[i]);
}
}
private static Byte readByte() throws IOException{
return new Byte(cdis.readByte());
}
private static Short readShort() throws IOException{
return new Short(cdis.readShort());
}
private static Integer readInt() throws IOException{
return new Integer(cdis.readInt());
}
private static Boolean readBoolean() throws IOException{
return new Boolean(cdis.readBoolean());
}
private static Character readChar() throws IOException{
return new Character(cdis.readChar());
}
private static String readString() throws IOException{
return cdis.readUTF();
}
private static int[] readIntArray() throws IOException{
int size = cdis.readInt();
int[] tmp = new int[size];
for(int i = 0; i < size; ++ i){
tmp[i] = cdis.readInt();
}
return tmp;
}
private static byte[] readByteArray() throws IOException{
int size = cdis.readInt();
byte[] tmp = new byte[size];
for(int i = 0; i < size; ++ i){
tmp[i] = cdis.readByte();
}
return tmp;
}
private static short[] readShortArray() throws IOException{
int size = cdis.readInt();
short[] tmp = new short[size];
for(int i = 0; i < size; ++ i){
tmp[i] = cdis.readShort();
}
return tmp;
}
private static boolean[] readBooleanArray() throws IOException{
int size = cdis.readInt();
boolean[] tmp = new boolean[size];
for(int i = 0; i < size; ++ i){
tmp[i] = cdis.readBoolean();
}
return tmp;
}
private static char[] readCharArray() throws IOException{
int size = cdis.readInt();
char[] tmp = new char[size];
for(int i = 0; i < size; ++ i){
tmp[i] = cdis.readChar();
}
return tmp;
}
private static String[] readStringArray() throws IOException{
int size = cdis.readInt();
String[] tmp = new String[size];
for(int i = 0; i < size; ++ i){
tmp[i] = cdis.readUTF();
}
return tmp;
}
public static void deleteRecrodInfo(int id){
try {
rec.deleteRecord(id);
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public static void deleteRecrodStore(){
closeRMS();
try {
RecordStore.deleteRecordStore(RMS_NAME1);
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
public static boolean isEmpty(){
try {
if(rec.getNumRecords() == 0)
return true;
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
}
return false;
}
// --------------------------------------------------------------------------------------------------------
public static byte getByte(int index){
if(index >= dataList.size()){
return 0;
}
switch(dataList.elementAt(index)){
case DATA_BYTE:
return ((Byte)data.elementAt(index)).byteValue();
}
return 0;
}
public static short getShort(int index){
if(index >= dataList.size()){
return 0;
}
switch(dataList.elementAt(index)){
case DATA_SHORT:
return ((Short)data.elementAt(index)).shortValue();
}
return 0;
}
public static int getInt(int index){
if(index >= dataList.size()){
return 0;
}
switch(dataList.elementAt(index)){
case DATA_INT:
return ((Integer)data.elementAt(index)).intValue();
}
return 0;
}
public static boolean getBoolean(int index){
if(index >= dataList.size()){
return false;
}
switch(dataList.elementAt(index)){
case DATA_BOOLEAN:
return ((Boolean)data.elementAt(index)).booleanValue();
}
return false;
}
public static char getChar(int index){
if(index >= dataList.size()){
return ' ';
}
switch(dataList.elementAt(index)){
case DATA_CHAR:
return ((Character)data.elementAt(index)).charValue();
}
return ' ';
}
public static String getString(int index){
if(index >= dataList.size()){
return "";
}
switch(dataList.elementAt(index)){
case DATA_STRING:
return (String)data.elementAt(index);
}
return "";
}
public static int[] getIntArray(int index){
if(index >= dataList.size()){
return new int[1];
}
switch(dataList.elementAt(index)){
case DATA_INT_ARRAY:
return (int[])data.elementAt(index);
}
return new int[1];
}
public static byte[] getByteArray(int index){
if(index >= dataList.size()){
return new byte[1];
}
switch(dataList.elementAt(index)){
case DATA_BYTE_ARRAY:
return (byte[])data.elementAt(index);
}
return new byte[1];
}
public static short[] getShortArray(int index){
if(index >= dataList.size()){
return new short[1];
}
switch(dataList.elementAt(index)){
case DATA_SHORT_ARRAY:
return (short[])data.elementAt(index);
}
return new short[1];
}
public static boolean[] getBooleanArray(int index){
if(index >= dataList.size()){
return new boolean[1];
}
switch(dataList.elementAt(index)){
case DATA_BOOLEAN_ARRAY:
return (boolean[])data.elementAt(index);
}
return new boolean[1];
}
public static char[] getCharArray(int index){
if(index >= dataList.size()){
return new char[1];
}
switch(dataList.elementAt(index)){
case DATA_CHAR_ARRAY:
return (char[])data.elementAt(index);
}
return new char[1];
}
public static String[] getStringArray(int index){
if(index >= dataList.size()){
return new String[1];
}
switch(dataList.elementAt(index)){
case DATA_STRING_ARRAY:
return (String[])data.elementAt(index);
}
return new String[1];
}
}