`
jimphei
  • 浏览: 38168 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

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

阅读更多

做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;
		}
	}
}

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics