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

📄 myrms.java

📁 星空大战,小车发出子弹打空中的飞行物。空中飞行物也 能发出子弹袭击小车。
💻 JAVA
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.rms.RecordStore;

//记录集
public class MyRMS
{
	private RecordStore rs;
	private String rsName;
	
	public MyRMS(String rsName)
	{
		this.rsName = rsName;
		openRS();
	}
	
	public RecordStore openRS()
	{	
		if(rsName.length()>32)
			return null;
		
		try
		{
			rs = RecordStore.openRecordStore(this.rsName, true);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return rs;
	}
	
	public void write(int[] data)
	{	
		try
		{
			openRS();
			
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(bos);
			
			for(int i=0; i<data.length; i++)
			{
				dos.writeInt(data[i]);
			}
			byte[] res = bos.toByteArray();
			
			rs.addRecord(res, 0, res.length);
			
			dos.close();
			
			rs.closeRecordStore();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void update(int[] data)
	{	
		try
		{

			
			if(rs.getNumRecords()<1)
			{
				write(new int[5]);
			}
			
			openRS();
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(bos);
			
			for(int i=0; i<data.length; i++)
			{
				dos.writeInt(data[i]);
			}
			byte[] res = bos.toByteArray();
			
			rs.setRecord(1, res, 0, res.length);
			
			dos.close();
			
			rs.closeRecordStore();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public int[] read()
	{
		int[] res = new int[5];
		
		try
		{

			
			if(rs.getNumRecords()<1)
			{
				write(new int[5]);
			}
			
			openRS();
			byte[] data = rs.getRecord(1);
			
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			DataInputStream dis = new DataInputStream(bis);

			for(int i=0; i<res.length; i++)
			{
				res[i] = dis.readInt();
			}
			 
			dis.close();
			
			rs.closeRecordStore();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return res;
	}
}

⌨️ 快捷键说明

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