欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于j2me中对象的存取操作,对象与字节的转化

程序员文章站 2022-05-08 19:22:06
...

做j2me的,应该经常会用的类似下面这样

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class StoreMidlet2 extends MIDlet {
	private RecordStore rs;

	public StoreMidlet2() throws Exception {

		Custom cus = new Custom("中国人", "32451674531", 23);
		try {
			rs=RecordStore.openRecordStore("myinfo", true);
			byte[] bytes=cus.objectToByteArray();
			rs.addRecord(bytes, 0, bytes.length);

			byte[] bytes2=rs.getRecord(1);
			Custom newcus=cus.byteArrayToObject(bytes2);
			System.out.println(newcus.getName());
		} catch (RecordStoreFullException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			rs.closeRecordStore();
		}
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
	}



	class Custom {
		private String name;
		private String phone;
		private int age;

		/**
		 * @return the name
		 */
		public String getName() {
			return name;
		}

		/**
		 * @return the phone
		 */
		public String getPhone() {
			return phone;
		}

		/**
		 * @return the age
		 */
		public int getAge() {
			return age;
		}

		/**
		 * @param name
		 *            the name to set
		 */
		public void setName(String name) {
			this.name = name;
		}

		/**
		 * @param phone
		 *            the phone to set
		 */
		public void setPhone(String phone) {
			this.phone = phone;
		}

		/**
		 * @param age
		 *            the age to set
		 */
		public void setAge(int age) {
			this.age = age;
		}

		public Custom() {

		}

		public Custom(String n, String p, int a) {
			this.name = n;
			this.phone = p;
			this.age = a;
		}
		public byte[] objectToByteArray() throws Exception{
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			DataOutputStream dos=new DataOutputStream(baos);
			dos.writeUTF(this.name);
			dos.writeUTF(this.phone);
			dos.writeInt(this.age);
			baos.close();
			dos.close();
			return baos.toByteArray();
		}
		public Custom byteArrayToObject(byte[] bytes) throws Exception{
			ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
			DataInputStream dis=new DataInputStream(bais);
			Custom cus=new Custom();
			cus.setName(dis.readUTF());
			cus.setPhone(dis.readUTF());
			cus.setAge(dis.readInt());
			dis.close();
			bais.close();
			return cus;
		}
	}
}

 的代码,不知还有没有别的方案来存取对象。

相关标签: DOS