Use Java to operate binary and
Two articles using Java operation text have introduced how to operate binary files and text files. In fact, there are DATA-based data operations in Java. The DATA refers to Java's basic data type and string. Basic data types include Byte, int, char, long, float, double, boolean, and short.
It is said that the two classes of Java must talk to DataInputStream and DataOutputStream. They provide operations for Java basic data types, but these methods are actually DataInput and DataOutput defined in two important interfaces, their functionality is to convert binary byte streams into Java's basic data type, while still The function of building String is built from data using UTF-8 encoding from data. There is an important class randomaccessfile to implement DataInput and DataOutput two interfaces allow him to write and read the files simultaneously.
The method in the two classes of DataInputStream and DataOutputStream is simple, the basic structure is readxxxx () and Writexxxx () where xxxx represents the basic data type or String. Not much here, but it is worth mentioning that we need to read the Unicode coding rules in Java and have a more detailed introduction in the API DOC. Usually our objects have many of the basic data types of Java, such as one person's information including name, electronic mailbox, phone number, and gender. In fact, we can use the method in DataInputStream and the method in DataOutputStream, read the data in accordance with the same sequence according to the same sequence, which is the serialization of our own, which can be used in data transfer. For example, serializing the data is used in the J2ME networking program. Let's take a look at how to realize serialization, first we have two constructor, one of which is empty. Public Account () {
}
Public Account (String Username, String Email, Int Age, Boolean Gender) {this.Username = Username; this.email = email; this.age = age; this.gender = gender;} is also very common when we serialize Simple, we just write to the member variables of the object in order to DataOutputStream. For example, public void serialize (DataOutputStream DOS) throws ioException {dos.writeutf (username); dos.writeutf (email); dos.writeint (agn); dos.writeBoolean (gender);} When we perform anti-sequence, follow The data is read from DataInputStream in the same order and assigns a member variable. E.g. public static Account deserialize (DataInputStream dis) throws IOException {Account account = new Account (); account.userName = dis.readUTF (); account.email = dis.readUTF (); account.age = dis.readInt (); Account.gender = dish.readboolean (); Return Accent;} In order to facilitate debugging We also provide a toString () method to print out the actual information of the object. This is a good habit. Public string toString () {return "username =" username "email =" email "age =" AGE "gender =" (GENDER? "Male": "female");} In order to test serialization We write the following procedures for testing, the code is relatively simple. Package com.j2medev.mingjava;
Import java.io. *;
Public class testdataio {
Public static void main (string [] args) throws ioException {Account = New Account ("Mingjava", "Eric.zhan@263.net", 25, true); System.out.Println ("Before Serialization ... ...... "); System.out.println (account.toString ()); ByteArrayOutputStream baos = new ByteArrayOutputStream (); DataOutputStream dos = new DataOutputStream (baos); account.serialize (dos); DataInputStream dis = new DataInputStream (new byterrayinputstream (baos.tobyteaRray ())))); Account Saccount = Account.deSerialize (DIS); System.Out.println ("After Serialization ........"); system.out.println (Saccount.Tostring ()); dos.close (); dis.close ();}} package com.j2medev.minjava; import java.io. *;
Public class account {private string username = ""; private string email = "; private int agent = 0; private bolean gender = false;
Public Account () {
}
Public Account (String Username, String Email, Int Age, Boolean Gender) {this.Username = Username; this.email = email; this.age = age; itute = gender;
Public void serialize (DOS.WRITEUTF (Username); dos.writeint (age); dos.writeboolean (GENDER);
public static Account deserialize (DataInputStream dis) throws IOException {Account account = new Account (); account.userName = dis.readUTF (); account.email = dis.readUTF (); account.age = dis.readInt (); account .gender = dispuit;} public string toString () {return "username =" username "email =" email "age =" AGE "gender =" (GENDER? " Male ":" female ");}} Compile running program in console output: Before serialization ......... username = mingjava email = Eric.zhan@263.net ageization .. ........ username = mingjava email = eric.zhan@263.net age = 25 gender = MALE Serialization success, then I will tell how to use serialization mechanisms in J2ME.