Basic data types and streams in Java

Time:2021-12-25

In addition to binary files and text files, there are data operations based on data in Java. Data here refers to the basic data type and string of Java. 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 you must talk about are datainputstream and dataoutputstream. They provide operations on java basic data types, but these methods are actually datainput and dataoutput defined in two important interfaces. Their function is to convert binary byte streams into java basic data types. At the same time, they also provide the function of building strings from data using UTF-8 coding. An important class RandomAccessFile implements two interfaces, datainput and dataoutput, so that it can write and read files at the same time.

The methods in datainputstream and dataoutputstream classes are simple. The basic structures are readxxxx() and writexxxx(), where XXXX represents the basic data type or string. Not much here, but it is worth mentioning that it is necessary to read the encoding rules of Unicode in Java, which are described in detail in API doc. Usually, many of our objects are composed of basic data types of Java, such as a person’s information, including name, e-mail, phone number and gender. In fact, we can use the methods in datainputstream and dataoutputstream to write data into the stream according to a certain sequence, and then read them out according to the same sequence. This is our own serialization, which can be used in data transmission, such as using serialization mechanism to transmit data in J2ME networking programs. Let’s see how to implement serialization by ourselves. First, we need to have two constructors, 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;
  }

When we serialize, it is also very simple. We just write the member variables of the object in order to the 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 data from datainputstream in the same order and assign it to member variables. 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;
  }

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 program 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”,”[email protected]”,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 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.readInt();
  account.gender = dis.readBoolean();
  return account;
  }

  public String toString()
  {
  return “UserName = ” + userName + ” Email = ” + email + ” age = ” + age + ” gender = ” + (gender ? “male” : “female”);
  }
  }

Compile and run the program and output it on the console:

  before serialization………
  UserName = mingjava Email = [email protected] age = 25 gender = male
  after serialization……….
  UserName = mingjava Email = [email protected] age = 25 gender = male

Serialization succeeded. Later, I will talk about how to use serialization mechanism in J2ME networking.