Operation method of basic data type and stream in Java

xiaoxiao2021-03-06  21

In addition to binary files and use text files, DATA is referring to the basic data type and String of Java. 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 agent) {this.username = username; this.email = email; this.ge = age; gnder = gender;} Serialization is also very simple, we just write to the member variables of the object in order to DataOutputStream. E.g:

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 same The order reads data from DataInputStream 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 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 account = new Account ( "mingjava", "eric.zhan@263.net" , 25, true); "Before Serialization ........."); system.out.println; ByteArrayoutputStream baos = new byterrayoutputstream (); DataOutputstream DOS = new DataOutputStream (baos); account.serialize (dos); DataInputStream dis = new DataInputStream (new ByteArrayInputStream (baos.toByteArray ())); Account sAccount = Account.deserialize (dis); System.out.println ( "after serialization ........ "); system.out.println (Saccount.toString ()); dos.close (); dis.close ();}} package com.j2medev.mingjava; import java. io *;. public class Account {private String userName = ""; private String email = ""; private int age = 0; private boolean gender = false; public Account () {} public Account (String userName, String email, int Age, Boolean Gender) {this.username = Username; this. email = email; this.age = age; this.gender = gender;} public void serialize (DataOutputStream dos) throws IOException {dos.writeUTF (userName); dos.writeUTF (email); 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 .read.gender = dish.readboolean (); returnec} public string toString () {Return "username =" username "email =" email "agn ="

AGE "gender =" (Gnder? "Male": "female");}} The compile run program is output in the console: Before Serialization ... 263.NET AGE = 25 Gender = MALEAFTER Serialization ........ username = mingjava email = Eric.zhan@263.net age = 25 gender = MALE serialization success!

转载请注明原文地址:https://www.9cbs.com/read-60455.html

New Post(0)