J2ME RMS 对象序列化存储

        需要在手机上保存很多好友的信息,每个Friend对象定义有Family,Name,Given Name,Age,Telephone等若干属性。RMS中引入对象序列化。所谓对象序列化,就是将对象的状态转换成字节流,以后可以通过这些值在生成相同状态的对象。整个转换过程需要ByteArrayOutputStream、DataOutputStream、ByteArrayInputStream、DataInputStream四个类的协助。

 

于是,我们就不需要将信息存好多列,只需要按照对象存储就可以了,就像数据库那样,每一条数据有多个字段。

 

 

现在上代码

MIDlet

package com.zeph.j2me.object.serilize;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * 对象序列化
 * 
 * @author Ben Zeph
 * 
 */
public class ObjectSerilizeMIDlet extends MIDlet {

	private Display display;

	private Form form;

	private TextField brandTextField;

	private TextField modelTextField;

	private TextField osTextField;

	private TextField priceTextField;

	public ObjectSerilizeMIDlet() {

		display = Display.getDisplay(this);

		form = new Form("对象序列化");

		brandTextField = new TextField("品牌", "", 10, TextField.ANY);

		modelTextField = new TextField("型号", "", 10, TextField.ANY);

		osTextField = new TextField("操作系统", "", 10, TextField.ANY);

		priceTextField = new TextField("价格", "", 10, TextField.NUMERIC);

		form.append(brandTextField);

		form.append(modelTextField);

		form.append(osTextField);

		form.append(priceTextField);

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

		display.setCurrent(form);

		SmartPhone phone = new SmartPhone("Apple", "iphone4s", "ios5", 7999);

		SmartPhoneRecordStore rs = new SmartPhoneRecordStore("SmartPhone");

		rs.addRecord(Serializer.serialize(phone));

		SmartPhone iphone4s = Serializer.deserialize(rs.getRecord(1));

		brandTextField.setString(iphone4s.getBrand());

		modelTextField.setString(iphone4s.getModel());

		osTextField.setString(iphone4s.getOs());

		priceTextField.setString(String.valueOf(iphone4s.getPrice()));

	}
}


 

Serialize

package com.zeph.j2me.object.serilize;

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

/**
 * 数据序列化工具类
 * 
 * @author Ben Zeph
 * 
 */
public class Serializer {

	public static byte[] serialize(SmartPhone phone) {

		byte[] data = null;

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		DataOutputStream dos = new DataOutputStream(baos);

		try {

			dos.writeUTF(phone.getBrand());

			dos.writeUTF(phone.getModel());

			dos.writeUTF(phone.getOs());

			dos.writeInt(phone.getPrice());

			dos.flush();

			data = baos.toByteArray();

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {
				if (baos != null) {

					baos.close();

					baos = null;

				}
				if (dos != null) {

					dos.close();

					dos = null;

				}
			} catch (IOException e) {

				e.printStackTrace();

			}

		}
		return data;
	}

	public static SmartPhone deserialize(byte[] data) {

		SmartPhone phone = new SmartPhone();

		ByteArrayInputStream bais = new ByteArrayInputStream(data);

		DataInputStream dis = new DataInputStream(bais);

		try {

			phone.setBrand(dis.readUTF());

			phone.setModel(dis.readUTF());

			phone.setOs(dis.readUTF());

			phone.setPrice(dis.readInt());

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				if (dis != null) {

					dis.close();

					dis = null;

				}

				if (bais != null) {

					bais.close();

					bais = null;

				}

			} catch (IOException e) {

				e.printStackTrace();

			}

		}
		return phone;
	}

}


 

Smartphone

package com.zeph.j2me.object.serilize;

/**
 * 智能手机
 * 
 * @author Ben Zeph
 * 
 */
public class SmartPhone {

	/** 品牌 */
	private String brand;

	/** 型号 */
	private String model;

	/** 操作系统 */
	private String os;

	/** 价格 */
	private int price;

	public SmartPhone() {

	}

	public SmartPhone(String brand, String model, String os, int price) {

		this.brand = brand;

		this.model = model;

		this.os = os;

		this.price = price;

	}

	public String getBrand() {

		return brand;

	}

	public void setBrand(String brand) {

		this.brand = brand;

	}

	public String getModel() {

		return model;

	}

	public void setModel(String model) {

		this.model = model;

	}

	public String getOs() {

		return os;

	}

	public void setOs(String os) {

		this.os = os;

	}

	public int getPrice() {

		return price;

	}

	public void setPrice(int price) {

		this.price = price;

	}

}


 

RecordStore

package com.zeph.j2me.object.serilize;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * 智能手机记录存储类
 * 
 * @author Ben Zeph
 * 
 */
public class SmartPhoneRecordStore {

	private RecordStore rs = null;

	private String RecordStoreName;

	public SmartPhoneRecordStore(String RecordStoreName) {

		this.RecordStoreName = RecordStoreName;

		try {

			rs = RecordStore.openRecordStore(this.RecordStoreName, true);

			rs.closeRecordStore();

		} catch (RecordStoreFullException e) {

			e.printStackTrace();

		} catch (RecordStoreNotFoundException e) {

			e.printStackTrace();

		} catch (RecordStoreException e) {

			e.printStackTrace();
		}
	}

	/**
	 * 添加记录
	 * 
	 * @param record
	 */
	public void addRecord(byte[] data) {

		try {

			rs = RecordStore.openRecordStore(this.RecordStoreName, true);

			rs.addRecord(data, 0, data.length);

			rs.closeRecordStore();

		} catch (RecordStoreNotOpenException e) {

			e.printStackTrace();

		} catch (RecordStoreFullException e) {

			e.printStackTrace();

		} catch (RecordStoreException e) {

			e.printStackTrace();
		}

	}

	/**
	 * 删除记录
	 * 
	 * @param id
	 */
	public void deleteRecord(int id) {

		try {

			rs = RecordStore.openRecordStore(this.RecordStoreName, true);

			rs.deleteRecord(id);

			rs.closeRecordStore();

		} catch (RecordStoreNotOpenException e) {

			e.printStackTrace();

		} catch (InvalidRecordIDException e) {

			e.printStackTrace();

		} catch (RecordStoreException e) {

			e.printStackTrace();

		}

	}

	/**
	 * 获取记录
	 * 
	 * @param id
	 * @return
	 */
	public byte[] getRecord(int id) {

		byte[] data = null;

		try {

			rs = RecordStore.openRecordStore(this.RecordStoreName, true);

			data = rs.getRecord(id);

			rs.closeRecordStore();

		} catch (RecordStoreNotOpenException e) {

			e.printStackTrace();

		} catch (InvalidRecordIDException e) {

			e.printStackTrace();

		} catch (RecordStoreException e) {

			e.printStackTrace();

		}

		return data;

	}

	/**
	 * 修改记录
	 * 
	 * @param record
	 * @param id
	 */
	public void setRecord(String record, int id) {

		byte[] data = record.getBytes();

		try {

			rs = RecordStore.openRecordStore(this.RecordStoreName, true);

			rs.setRecord(id, data, 0, data.length);

			rs.closeRecordStore();

		} catch (RecordStoreNotOpenException e) {

			e.printStackTrace();

		} catch (InvalidRecordIDException e) {

			e.printStackTrace();

		} catch (RecordStoreException e) {

			e.printStackTrace();

		}

	}
}


 

 

 

你可能感兴趣的:(J2ME)