In one of the series, we focused on the basics of Record Management System. Before you introduce how to use the Record Management System, I want to introduce Java IO and sequential content in the J2ME platform, whether the above content is very important regardless of the General Networking framework for Record Management System or MIDP.
Java IO defined in CLDC is very short, but also provides enough classes to complete our IO operation. Since the implementation of J2SE is common, you can communicate with J2ME and J2SE or J2EE platforms. For example, communicate with the network and servlet. In the Record Management System, our primary use class is ByteArrayInputStream, ByteArrayoutputStream, DataInputStream, and DataOutputStream. The front is based on bytes, and the role of ByteArrayInputStream is to convert byte arrays to flow and the role of ByteArrayoutputStream is to convert data within the memory buffer into bytes. The latter two classes are based on the Java basic data type and String operation. Usually they transfer the two classes as a parameter to the constructor so they can read and write the basic data types and String. It is worth noting that byterrayoutputstream's TobyTearRay () method is to copy data in memory, such a waste of memory, in order to more effectively use limited storage space you can extend the ByteArrayoutputSteam class and then provide a getByTearray () method , Below is: Public class mybyteaRayoutputstream Extends ByteArrayoutputstream {............................................ GetByteArray () {Return buf;}}
There is no mechanism for object serialization in J2ME, but we can implement it yourself. Please consider the following class: //Bank.javapublic class Bank {private String bankName; private String phone; private int employeeNum; public Bank () {} public Bank (String aBankName, String aPhone, int aEmployeeNum) {this.bankName = aBankName ; this.phone = aPhone; this.employeeNum = aEmployeeNum;} public String getBankName () {return bankName = null bankName:!? "";} public String getPhone () {return phone = null phone:!? "";} Public int geTemployeenum () {return EMPLOYEENUM;
} We add two ways to this class to implement object serialization. As follows: public class Bank {private String bankName; private String phone; private int employeeNum; public Bank () {} public Bank (String aBankName, String aPhone, int aEmployeeNum) {this.bankName = aBankName; this.phone = aPhone ; this.employeeNum = aEmployeeNum;} public String getBankName () {return bankName = null bankName:!? "";} public String getPhone () {return phone = null phone:!? "";} public int getEmployeeNum () { return employeeNum;} public byte [] serialize () throws IOException {ByteArrayOutputStream bos = new ByteArrayOutputStream (); DataOutputStream dos = new DataOutputStream (bos); dos.writeUTF (getBankName ()); dos.writeUTF (getPhone ()); dos .writeInt (getEmployeeNum ()); dos.flush (); return bos.toByteArray ();} public Bank deserialize (byte [] data) throws IOException {ByteArrayInputStream bis = new ByteArrayInputStream (data); DataInputStream dis = new DataInputStream (bis ); Bank mybank = new bank (); mybank.bankname = disp.readutf (); mybank.phone = disp.re AduTf (); mybank.employeenum = dispurn mybank;} This we have realized the sequence of objects, which is also very simple to use. Serialized and reverse selecinstened operations are as follows: Bank Abank = .....; RecordStore RS = .....; try {byte [] data = ABANK.SERIALIZE (); rs.addrecord (Data , 0, Data.Length);} catch (ioException e) {// do something}
Catch (RecordStoreException E) {// do something} ------------------------------------------------------------------------------------------------------------------------------------------------------------
Byte [] data = ........; Bank Abank = null; try {abank = Bank.deSerialize (DATA);} catch (ooException e) {} is worth noting in the Bank class The members are all basic data types and String types, and there is no reference to other objects. This is the most ideal and simplest situation. In fact, we should try to do so when we design sequential classes in J2ME, avoid Don't have trouble.