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

📄 rmsutil.java

📁 这是个j2me中的RMS的例子,但是如果rms存储的太多数据,那么速度会很慢的
💻 JAVA
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.rms.RecordStore;


public class RmsUtil {

	public RmsUtil()
	{
		
	}
	
	public void createRS(String RSname)
	{
		RecordStore rs;
		try
		{
			rs=RecordStore.openRecordStore(RSname, true);
			rs.closeRecordStore();
		}catch(Exception e) 
		{
			System.out.println(e);
		}
	}
	
	public void destroyRS(String RSname)
	{
//		RecordStore rs;
		try
		{
//			rs=RecordStore.openRecordStore(RSname, true);
			RecordStore.deleteRecordStore(RSname);
//			int len=rs.getNumRecords();
//			for(int i=1;i<=len;i++)
//			{
//				rs.setRecord(i, null, 0, 0);
//			}
//			rs.closeRecordStore();
		}catch(Exception e) {}
	}
	
	public byte[] StringToByte(String name,int score,String note)
	{
		byte[] result=null;
		try
		{
			ByteArrayOutputStream bos=new ByteArrayOutputStream();
			DataOutputStream dos=new DataOutputStream(bos);
			
			dos.writeUTF(name);
			dos.writeInt(score);
			dos.writeUTF(note);
			result=bos.toByteArray();
			
			bos.close();
			dos.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public String[] ByteArarryToString(byte []bb)
	{
		String readResult[]=new String[3];
		try
		{
			ByteArrayInputStream bis=new ByteArrayInputStream(bb);
			DataInputStream dis=new DataInputStream(bis);
			
			readResult[0]=dis.readUTF();
			readResult[1]=String.valueOf(dis.readInt());
			readResult[2]=dis.readUTF();
			
			bis.close();
			dis.close();
		}catch(Exception e) {}
		return readResult;
	}
	
	public int AddDate(String RSname,String name,int score,String note)
	{
		RecordStore rs;
		int id=0;
		try
		{
			rs=RecordStore.openRecordStore(RSname, false);
			byte tmp[]=StringToByte(name, score, note);
			id=rs.addRecord(tmp, 0, tmp.length);
		}catch(Exception e) {}
		return id;
	}
	
	public void SetDate(String RSname,int id,String name,int score,String note)
	{
		RecordStore rs;
		try
		{
			rs=RecordStore.openRecordStore(RSname, false);
//			System.out.println("name = " + name + "score = " + score + "note :" + note);
			byte tmp[]=StringToByte(name, score, note);
//			System.out.println("id = " + id );
			rs.setRecord(id, tmp, 0, tmp.length);
		}catch(Exception e) {
			
		}
	}
	
	public void DeleteDate(String RSname,int id)
	{
		RecordStore rs;
		try
		{
			rs=RecordStore.openRecordStore(RSname, false);
			rs.deleteRecord(id);
		}catch(Exception e) {}
	}
	
	public String[] readDate(String RSname,int id)
	{
		RecordStore rs;
		String[] ss=new String[3];
		try
		{
			rs=RecordStore.openRecordStore(RSname, false);
			byte tmp[]=rs.getRecord(id);
			ss=ByteArarryToString(tmp);
		}catch(Exception e) {}
		return ss;
	}
	
}

⌨️ 快捷键说明

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