Basic data types and stream _jsp programming in Java

Source: Internet
Author: User
Tags serialization
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 account ()
{

}

Public account (String userName, string e-mail, int age, Boolean gender)
{
This.username = UserName;
This.email = email;
This.age = age;
This.gender = gender;
}

It's also very simple when we serialize, and we just write the object's member variables in order in DataOutputStream. For example:

public void serialize (DataOutputStream dos) throws IOException
{
Dos.writeutf (UserName);
Dos.writeutf (email);
Dos.writeint (age);
Dos.writeboolean (gender);
}

When we deserialize, we read the data from the DataInputStream in the same order and assign the value to the member variable. For example:

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 = Dis.readboolean ();
return account;
}

For ease of debugging We also provide an ToString () method to print out the actual information of the object. It's 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 program to test, 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);
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 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 e-mail, 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.readint ();
Account.gender = Dis.readboolean ();
return account;
}

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.

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.