Basic data types and stream operations in Java

Source: Internet
Author: User
Tags serialization
In Java, besides binary files and text files, there are Data-based Data operations. Data here refers to the basic Data types and strings of Java. Basic data types include byte, int, char, long, float, double, boolean, and short.
Speaking of the basic data types of Java, two classes must be mentioned: DataInputStream and DataOutputStream. They provide operations on the basic data types of Java, but these methods are actually the DataInput and DataOutput defined in two important interfaces, their function is to replace the binary byte flow with the basic data type of Java, and also provides the function to build a String using UTF-8 encoding from the data. There is an important class RandomAccessFile that implements the DataInput and DataOutput interfaces so that it can write and read files at the same time.
The methods in DataInputStream and DataOutputStream are simple. The basic structure is readXXXX () and writeXXXX (). XXXX indicates the basic data type or String. It is not described here, but it is worth mentioning that we need to read the unicode encoding rules in java, which are described in detail in the API doc. Generally, many of our objects are composed of the basic data types of java. For example, a person's information includes name, email box, phone number, and gender. In fact, we can use the methods in DataInputStream and the methods in DataOutputStream to write data into the stream in a certain sequence and then read the data in the same sequence. This is our own serialization, this can be used in data transmission. For example, the serialization mechanism is used to transmit data in the j2rnet program. Let's take a look at how to implement serialization by ourselves. First, we need to have two constructors, one of which is null.
Public Account ()
{
}
Public Account (String userName, String email, int age, boolean gender)
{
This. userName = userName;
This. email = email;
This. age = age;
This. gender = gender;
}
It is also very easy to serialize. We just write the object's member variables to DataOutputStream in order. For example:
Public void serialize (DataOutputStream dos) throws IOException
{
Dos. writeUTF (userName );
Dos. writeUTF (email );
Dos. writeInt (age );
Dos. writeBoolean (gender );
}
When deserialization is performed, data is read from DataInputStream in the same order and assigned to the member variable. For example:
Public static Account deserialize (DataInputStream dis) throws IOException
{
Account account = new Account ();
Account. userName = dis. readUTF ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.