1. There are several types of flows in Java

  • By stream direction: inputStream and outputStream
  • Node flow (data can be read and written from or to a specific place (node). Such as FileReader) and processing stream (is a connection and encapsulation of an existing stream, through the encapsulated stream function calls to achieve data reading and writing, such as BufferedReader processing stream constructor always takes another stream object as an argument. A stream object wrapped multiple times by other streams is called a stream link.
  • In terms of units of data processing: byte stream and character stream. Byte streams inherit from InputStream and OutputStream, and character streams inherit from InputStreamReader and OutputStreamWriter.



2. How do byte streams become character streams

  • Byte InputStream character input flows through the InputStreamReader implementation, whose constructor can pass in an InputStream object.
  • Byte OutputStream character output flows through the OutputStreamWriter implementation, whose constructor can pass in an OutputStream object.

Classes that can be serialized in Java must first implement the Serializable interface, which has no abstract methods but serves as a marker.

// Object output stream
ObjectOutputStream objectOutputStream =
new ObjectOutputStream (new FileOutputStream(new File ("D://obj")));
objectOutputStream.writeObject (new User (" zhangsan".100)); objectOutputStream.close();// Object input stream
ObjectInputStream objectInputStream =
new ObjectInputStream(new FileInputStream(new File ("D://obj")));
User user = (User) objectInputstream.readObject();
System.out.println(user);
objectInputStream.close();
Copy the code

4. The difference between byte stream and character stream

  • When a byte stream is read, a byte is returned when a byte is read. A character stream reads one or more bytes (two bytes in Chinese, three bytes in UTF-8 code table) using a byte stream. The specified encoding table is searched first, and the characters found are returned. Byte streams can handle all types of data, such as images, MP3s,AVI video files, while character streams can only handle character data. Whenever dealing with plain text data, character streams are preferred and byte streams are used everywhere else. The byte stream mainly operates on byte data, based on byte arrays. The class to operate on is OutputStream.inputStream
  • The character stream processing unit is a 2-byte Unicode character that manipulates characters, character arrays, or strings, respectively, while the byte stream processing unit is a 1-byte unit that manipulates bytes and byte arrays. So a character stream is a Java virtual machine that converts bytes into 2-byte Unicode characters, so it’s good for multiple languages! If it is audio file, picture, song, use byte stream, if it is related to Chinese (text), use character stream. In a program where a character is equal to two bytes, Java provides Reader. Writer classes for manipulating character streams.

5. What is Java serialization and how is it implemented? Serialization is a mechanism for dealing with streams of objects, that is, streaming the contents of objects. You can read and write the fluidized object or transfer the fluidized object between networks. Serialization is intended to solve problems that arise when reading or writing to a stream of objects. Implementation of serialization: Implements the Serializable interface for classes that need to be serialized. The interface has no methods that need to be implemented, implements Serializable only to mark the object as Serializable, and then uses an output stream (e.g. FileOutputStream) to construct an ObjectOutputStream, and then, The writeObject(Object obj) method of the ObjectOutputStream Object can be used to write an Object with argument obj (that is, to save its state) and to restore it with an input stream. When two processes are communicating remotely, they can send each other various types of data. Data of any type is sent over the network as binary sequences. The sender needs to convert this Java object into a sequence of bytes before it can be sent over the network; The receiver needs to restore the byte sequence back to Java objects. Only objects that support the Java.io.Serializable interface can be written to a stream. The classes of each Serializable object are encoded, including the class name and class signature, the object’s field and array values, and closures for all other objects referenced from the original object.

  • concept

Serialization: The process of converting Java objects into byte sequences. Deserialization: The process of restoring a sequence of bytes to Java objects.

  • use

Object serialization has two main uses: 1) to permanently store the byte sequence of an object on hard disk, usually in a file; 2) Transmit the byte sequence of the object over the network.

  • Serialization API

Java. IO. ObjectOutputStream represents the output stream Object, its writeObject (Object obj) methods for parameter specifies the obj Object serialization, to get the sequence of bytes to a target output stream. Only objects of classes that implement the Serializable and Externalizable interfaces can be serialized. Java. IO. ObjectInputStream representative object input stream, its readObject () method from the source of a sequence of bytes read from the input stream, deserialize them as an object, and returns it.

Code sample

import java.io.*;
import java.util.Date;

public class ObjectSaver {
    public static void main(String[] args) throws Exception {
        /* D:\\ objectfile. obj represents the file that holds the serialized object */


        // serialize objects
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\objectFile.obj"));
        Customer customer = new Customer("Pockmarked seed".24);
        out.writeObject("Hello!");    // Write a literal constant
        out.writeObject(new Date());    // Write an anonymous Date object
        out.writeObject(customer);    // Write the customer object
        out.close();


        // Deserialize the object
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\objectFile.obj"));
        System.out.println("obj1 " + (String) in.readObject());    // Read literal constants
        System.out.println("obj2 " + (Date) in.readObject());    // Read the anonymous Date object
        Customer obj3 = (Customer) in.readObject();    // Read the Customer object
        System.out.println("obj3 "+ obj3); in.close(); }}class Customer implements Serializable {
    private String name;
    private int age;
    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString(a) {
        return "name=" + name + ", age="+ age; }}Copy the code

The execution result

  • Note: Read objects in the same order as they were written. The default serialization mechanism for an object writes the object’s class, its class signature, and the values of non-transient and non-static fields.