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

📄 datarms.java

📁 这个是早期学习时写的RPG游戏 包括地图相关 战斗相关 NPC与 存储等 功能
💻 JAVA
字号:
package perGame;

import javax.microedition.rms.RecordStore;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.*;
import java.io.*;

public class DataRMS {
    MyCanvas mc;
    RecordStore rs;
    String name = "rsName";
    public DataRMS(MyCanvas mc) {
        this.mc = mc;
    }

    public void saveFile() {
        this.rs = null;
        this.rs = openAnyWay(this.name);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); //字节数组输出流
        DataOutputStream dos = new DataOutputStream(bos); //带格式的输出流
        byte data[]; //字节数组
        try {

            //往数据库里面写东西
            short store[][] = new short[3][200];
            store = this.proReadFile();



                store[1][0] = (short) mc.hero.x;
                store[1][1] = (short) mc.hero.y;
                store[1][2] = (short) mc.mapID;







            for (int i = 0; i < store.length; i++) {
                for (int j = 0; j < store[i].length; j++) {
                    dos.writeShort(store[i][j]);
                }
            }
            data = bos.toByteArray(); //转换为字节数组
            if (rs.getNumRecords() == 0) {
                rs.addRecord(data, 0, data.length); //添加纪录
            } else {
                rs.setRecord(1, data, 0, data.length); //修改纪录
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.closeMyRMS();

    }

    public void readFile() {
        short store[][] = new short[3][200];
        store = this.proReadFile();
        this.rs = null;
        this.rs = this.openAnyWay(this.name);
        byte data[];
        try {
            data = rs.getRecord(1);
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            DataInputStream dis = new DataInputStream(bis);




                mc.hero.x = store[1][0];
                mc.hero.y = store[1][1];
                mc.mapID = store[1][2];



//            mc.h1.x = dis.readInt();
//            mc.h1.y = dis.readInt();
//            mc.mapId = dis.readInt();

        } catch (InvalidRecordIDException ex) {
        } catch (RecordStoreNotOpenException ex) {
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        this.closeMyRMS();

    }

    private void closeMyRMS() {
        try {
            rs.closeRecordStore();
        } catch (RecordStoreNotOpenException ex) {
        } catch (RecordStoreException ex) {
        }
    }

    private RecordStore openAnyWay(String name) {

        if (name.length() > 32) {
            return null;
        }
        RecordStore rst = null;
        try {
            rst = RecordStore.openRecordStore(name, true); //打开数据库
        } catch (RecordStoreException ex) {
        }
        return rst;
    }

    public short[][] proReadFile() {
        short store[][] = new short[3][200];
        this.rs = null;
        this.rs = this.openAnyWay(this.name);
        byte data[];
        try {
            data = rs.getRecord(1);
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            DataInputStream dis = new DataInputStream(bis);
            for (int i = 0; i < store.length; i++) {
                for (int j = 0; j < store[i].length; j++) {
                    store[i][j] = dis.readShort();
                }
            }

        } catch (InvalidRecordIDException ex) {
        } catch (RecordStoreNotOpenException ex) {
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return store;

    }

}

⌨️ 快捷键说明

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