In Java, in addition to binary files and the use of text files, there is data based operations, where data refers to the Java basic data types and string. Basic data types include byte, int, char, long, float, double, Boolean, and short.
When it comes to Java's basic data types, the two classes that must be mentioned are DataInputStream and DataOutputStream. They provide operations on Java basic data types, but these methods are in fact datainput and dataoutput defined in two important interfaces, and their function is to convert binary byte flows to Java basic data types. It also provides the ability to build a string from the data using UTF-8 encoding. An important class Randomaccessfile implements the Datainput and dataoutput two interfaces that enable him to write and read files simultaneously.
The methods in the DataInputStream and DataOutputStream two classes are simple, and the basic structure is readxxxx () and writexxxx () where XXXX represents the base data type or string. Not much to tell here, but it is worth mentioning that we need to read the code rules for Unicode in Java, which is described in more detail in the API Doc. Usually many of our objects are made up of Java's basic data types, such as a person's information including name, e-mail, phone number, and gender. In fact, we can use the method in the DataInputStream and the method in the DataOutputStream to follow a certain sequence to write the data into the stream and then read them out in the same sequence, this is our own implementation of serialization, which can be used in data transmission, For example, use serialization mechanism to transfer data in J2ME networking programs. Let's look at how we can serialize ourselves, and first we have two constructors where one of the arguments is empty.
Public String toString ()
{
return "UserName =" + UserName + "email =" + email + ' age = ' + age + "gender =" + (gender?) Male ":" female ");
}
}
The compiler runs the program to output at the console:
Before serialization .....
UserName = Mingjava Email = eric.zhan@263.net age = gender = Male
After serialization .....
UserName = Mingjava Email = eric.zhan@263.net age = gender = Male
Serialization succeeded, and I'll explain how to use the serialization mechanism in J2ME networking.
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.