This is the 16th day of my participation in Gwen Challenge

Java IO stream

  • IO is short for Input/Output. I/O is a very practical technology that processes data transfer between devices. Such as reading/writing documents, network communication, etc.
  • In Java programs, input and output operations on data are performed as “streams”.
  • The java.io package provides a variety of “stream” classes and interfaces to retrieve different kinds of data and input or output data through standard methods.

The classification of the flow

Operation data unit: byte stream, character stream

  • For text files (.txt,.java,.c,.cpp), character streams are used
  • For the text files (…. JPG, mp3, mp4, avi,. Doc,. PPT,…). , using byte stream processing

Data flow: input flow, output flow

  • Input Input Reads external data (data from storage devices, such as disks and CDS) into a program (memory).
  • Output Output Outputs program (memory) data to storage devices, such as disks and CD-RoMs.

The role of flow: node flow, processing flow

Node flow: Reads and writes data directly from the data source or destination.

Processing stream: Not directly connected to a data source or destination, but “connected” to an existing stream (node stream or processing stream), providing the program with more powerful read/write capabilities through the processing of data.

Several commonly used IO stream structures

Abstract base class Node flow (or file flow) Buffered flow (a type of processing flow)
InputStream FileInputStream (read(byte[] buffer)) BufferedInputStream (read(byte[] buffer))
OutputSteam FileOutputStream (write(byte[] buffer,0,len) BufferedOutputStream (write(byte[] buffer,0,len) / flush()
Reader FileReader (read(char[] cbuf)) BufferedReader (read(char[] cbuf) / readLine())
Writer FileWriter (write(char[] cbuf,0,len) BufferedWriter (write(char[] cbuf,0,len) / flush()

Description of abstract base classes

Abstract base class Byte stream Characters of the flow
The input stream InputSteam Reader
The output stream OutputSteam Writer
  • The lO flow in Java involves more than 40 classes, which are derived from the following four abstract base classes.
  • The subclass names derived from these four classes are all suffixed with the name of their parent class.

InputSteam&Reader

InputStream and Reader are the base classes for all input streams.

  • InputStream (typical implementation: FileInputStream)

  • Reader (Typical implementation: FileReader)

File IO resources opened in the program do not belong to the memory resources, garbage collection mechanism cannot reclaim the resources, so you should explicitly close the file IO resources.

A FileInputStream gets input bytes from a file in the file system. FileInputStream is used to read raw byte streams such as non-text data. To read a character stream, you need to use FileReader.


InputSteam:

  • int read()

    Reads the next byte of data from the input stream. Returns an int byte value in the range 0 to 255. If no bytes are available because the end of the stream has been reached, the value -1 is returned.

  • int read(byte[] b)

    Read up to B.length bytes of data from this input stream into a byte array. If no bytes are available because the end of the stream has been reached, the value -1 is returned. Otherwise, the number of bytes actually read is returned as an integer.

  • int read(byte[] b,int off,int len)

    Reads up to len data bytes from the input stream into a byte array. An attempt was made to read len bytes, but it is possible to read less than this value. Returns the number of bytes actually read as an integer. If there are no bytes available because the stream is at the end of the file, the value -1 is returned.

  • public void close throws IOException

    Close this input stream and release all system resources associated with it.

Reader:

  • int read()

    Read a single character. Characters read as integers, ranging from 0 to 65535 (0x00-0xFFFF) (2-byte Unicode code), or -1 if the end of the stream has been reached.

  • Int read (char[] cbuf)

    Reads a character into an array. If the end of the stream has been reached, -1 is returned. Otherwise return the number of characters read this time.

  • Int read (char[] cbuf,int off,int len)

    Reads a character into a part of an array. Store in array cbuf, starting at off, and read up to len characters. If the end of the stream has been reached, -1 is returned. Otherwise return the number of characters read this time.

  • public void close throws IOException

    Close this input stream and release all system resources associated with it

OutputSteam&Writer

Writer uses characters as the operation unit directly, so you can replace character arrays with strings, that is, strings as arguments.

FileOutputStream gets output bytes from a file in the file system. FileOutputstream is used to write out raw byte streams such as non-text data. To write out character streams, use FileWriter

OutputStream:

  • void write(int b)

    Writes the specified byte to this output stream. The general convention for write is to write a byte to the output stream. The bytes to be written are the eight bits of parameter B. The 24 high values of B will be ignored. The value ranges from 0 to 255

  • Void write (byte[] b)

    Writes B. length bytes from the specified byte array to this output stream. The general convention for write (b) is that it should have exactly the same effect as calling wite (b,0,b.length).

  • Void write (byte[] b,int off,int len)

    Writes len bytes from the specified byte array starting with the offset off to the output stream.

  • public void flush()throws IOException

    Flush this output stream and force all buffered output bytes to be written out, calling this method to indicate that these bytes should be written immediately to their intended destination.

  • public void close throws IOException

    Close this export stream and release all system resources associated with it.

Writer:

  • void write(int c)

    Writes a single character. The character to be written is contained in the 16 bits of the given integer value, and the 16 bits are ignored. That is, write the Unicode code between 0 and 65535.

  • void write(char[] cbuf)

    Write character array

  • void write(char[] cbuf,int off,int len)

    Writes a part of a character array. Starting with off, len characters are written

  • void write(String str)

    Write a string.

  • void write(String str,int off,int len)

    Writes a part of a string.

  • void flush()

    Flush the buffer for the flow, and they are immediately written to the desired destination.

  • public void close throws IOException

    Close this output stream and release all system resources associated with it

Input and output standardization process

Input process

① Create an object of class File that specifies the source of the data to be read. (This file must exist)

(2) Create an input stream, passing an object of the File class into the stream constructor

Create byte[] or char[].

4. Shut down flow resources

Note: Exceptions that occur ina program need to be handled using try-catch-finally.

The output process

① Create an object of class File that specifies the location of the written data. (This file is not required to exist)

(2) Create an output stream, passing an object of the File class into the stream constructor

Write (char[]/byte[] buffer,0,len)

4. Shut down flow resources

Note: Exceptions that occur ina program need to be handled using try-catch-finally.

Node flow (file flow)

File input FileReader

To read from a file into memory

Steps:

  1. Create a stream object to load an existing file into the streamFileReader fr = new FileReader(new File("Test. txt"));
  2. Create a temporary array to hold datachar[] ch = new char[1024];
  3. Call the read method of the stream object to read the data from the stream into an array.fr.read(ch);
  4. Close the resource.fr.close();
public void testFileReader(a)  {
    FileReader fr = null;
    try {
        //1. Instantiate the File class
        File file = new File("hello.txt");

        //2. Instantiate the FileReader stream
        fr = new FileReader(file);

        //3. Read in
        //read(char[] cbuf): Returns the number of characters read into the cBUF array each time. If the end of the file is reached, -1 is returned
        char[] cbuf = new char[5];
        int len;
        while((len = fr.read(cbuf)) ! = -1){
            String str = new String(cbuf,0,len); System.out.print(str); }}catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(fr ! =null) {//4. Resource shutdown
            try {
                fr.close();
            } catch(IOException e) { e.printStackTrace(); }}}}Copy the code

Pay attention to

  1. Read () : Returns a character read in. If the end of the file is reached, -1 is returned
  2. Exception handling: To ensure that the flow resource can be closed. A try-catch-finally is required
  3. The read file must exist or FileNotFoundException will be reported.

The output of the file FileWriter

From memory (program) to hard disk file

Steps:

  1. Create stream object, create data store fileFileWriter fw = new FileWriter(new File("Test.txt"))
  2. Calls the write method of the stream object to write data to the streamfw.write("HelloWord")
  3. Close the stream resource and empty the data from the stream into a file.fw.close()
public void testFileWriter(a) {
    FileWriter fw = null;
    try {
        //1. Provide an object of class File that specifies the File to be written to
        File file = new File("hello1.txt");

        //2. Provide the FileWriter object for writing data
        fw = new FileWriter(file,false);

        //3. Write operations
        fw.write("I have a dream! \n");
        fw.write("you need to have a dream!");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //4. Close the stream resource
        if(fw ! =null) {try {
                fw.close();
            } catch(IOException e) { e.printStackTrace(); }}}}Copy the code

FileInputSteam

FileOutputSteam

Almost the same as the character stream operation.

Example: Image file copy operation

import java.io.*;

/ * * *@authorHappy heart lake *@date2020/6/13 so * * /
public class Test {

    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(new File("test2.jpg"));
            fileOutputStream = new FileOutputStream(new File("F:\\VM"."test2.png"));
            // The process of copying images
            byte[] bytes = new byte[1024];
            while(fileInputStream.read(bytes)! = -1){ fileOutputStream.write(bytes); }}catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileInputStream ! =null) {
                try {
                    fileInputStream.close();
                } catch(IOException e) { e.printStackTrace(); }}if(fileOutputStream ! =null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

Pay attention to

  • You can use slash or \ to define a path.
  • Output operation, the corresponding File can not exist. No exceptions are reported.
  • File If the File corresponding to the hard disk does not exist, the File will be automatically created during the output.
  • File Indicates the File on the hard disk.
    • FileWriter(file,false)/FileWriter(file) overwrites the original file if the stream uses the constructor FileWriter(file,false).
    • If the stream uses the following constructor: FileWriter(file,true) does not overwrite the original file, but appends to it.
  • Ensure that the file exists when you read it. Otherwise, an exception is reported.
  • For text files (.txt,.java,.c,.cpp), character streams are used
  • For the text files (…. JPG, mp3, mp4, avi,. Doc,. PPT,…). , using byte stream processing

Buffer flow (main)

  • BufferedInputStream
  • BufferedOutputStream
  • BufferedReader
  • BufferedWriter

Effect: Improves the read and write speed of streams

Reason for improved read/write speed: Internal provides a buffer. The default is 8kb

When data is read, the data is read into the buffer in blocks, and subsequent reads directly access the buffer.

When reading a byte file with BufferedInputStream, BufferedInputStream reads 8192 bytes (8Kb) from the file at a time, stored in the buffer until the buffer is full, and then reads the next 8192-byte array from the file again.

When you write a byte to a stream, you don’t write it to a file, you write it to a buffer until the buffer is full, and then BufferedOutputStream writes the data in the buffer to the file all at once.

Flush () forces the entire contents of the buffer to be written to the output stream.

The sequence of closing streams is reversed from that of opening streams. As long as the outermost layer flow is closed, closing the outermost layer flow will also close the inner node flow.

Use of the flush() method: Manually write the contents of the buffer to a file.

If it is the close() method on a buffered stream object, it will not only close the stream, but also flush the buffer before closing the stream, after which it cannot be written.


Copy non-text files using BufferInputStream and BufferOutputStream

import java.io.*;

/ * * *@authorHappy heart lake *@date2020/6/13 so * * /
public class Test {

    public static void main(String[] args) {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            // Create node flow
            FileInputStream fileInputStream = new FileInputStream(new File("test2.jpg"));
            FileOutputStream fileOutputStream = new FileOutputStream(new File("F:\\VM"."test8.png"));
            // Create buffer flow
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            // Start copying
            byte[] bytes = new byte[1024];
            while((len = bufferedInputStream.read(bytes)) ! = -1) {
                bufferedOutputStream.write(bytes,0,len); }}catch (IOException e) {
            
            e.printStackTrace();
        } finally {
            if(bufferedInputStream ! =null) {
                try {
                    bufferedInputStream.close();
                } catch(IOException e) { e.printStackTrace(); }}if(bufferedOutputStream ! =null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

Use BufferedReader and BufferedWriter to copy text files

import java.io.*;

/ * * *@authorHappy heart lake *@date2020/6/13 so * * /
public class Test {

    public static void main(String[] args) {
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            // Create node flow
            FileReader fileReader = new FileReader(new File("test2.txt"));
            FileWriter fileWriter = new FileWriter(new File("F:\\VM"."test2.txt"));
            // Create buffer flow
            bufferedReader = new BufferedReader(fileReader);
            bufferedWriter = new BufferedWriter(fileWriter);
            // Start copying
            String data;
            while((data = bufferedReader.readLine()) ! =null) { bufferedWriter.write(data); bufferedWriter.newLine(); }}catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader ! =null) {
                try {
                    bufferedReader.close();
                } catch(IOException e) { e.printStackTrace(); }}if(bufferedWriter ! =null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Copy the code

Transformation flows

Conversion streams provide conversion between byte streams and character streams

The Java API provides two conversion flows:

  • InputstreamReaderWill:InputstreamconvertReader
  • OutputStreamWriterWill:WriterconvertOutputStream

Converting to a character stream is more efficient when the data in the byte stream is all characters.

A lot of times we use conversion streams to deal with file garbled problems. Realize the function of encoding and decoding.

InputStreamReader

InputStreamReader converts a byte input stream to a character input stream.

Decode: byte, byte array –> character array, string

The constructor:

  • public InputStreamReader(InputStream in)
  • Public InputStreamReader(Inputstream in,String charsetName

OutputStreamWriter

OutputStreamWriter converts an output stream of one character to an output stream of bytes.

Encoding: character array, string –> byte, byte array

The constructor:

  • public OutputStreamWriter(OutputStream out)
  • Public OutputStreamWriter(Outputstream out,String charsetName)// Encoding set can be specified

Note: The way the file is encoded (e.g., GBK) determines the character set used for parsing (only GBK).

/** Use InputStreamReader and OutputStreamWriter
@Test
public void test1(a) {
    InputStreamReader isr = null;
    OutputStreamWriter osw = null;
    try {
        //1. Create files and streams
        File file1 = new File("dbcp.txt");
        File file2 = new File("dbcp_gbk.txt");

        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);

        isr = new InputStreamReader(fis, "utf-8");
        osw = new OutputStreamWriter(fos, "gbk");

        //2. Read and write process
        char[] chars = new char[20];
        int len;
        while((len = isr.read(chars)) ! = -1){
            osw.write(chars,0,len); }}catch (IOException e) {
        e.printStackTrace();
    } finally {
        / / 3. The flow
        if(isr ! =null) {try {
                isr.close();
            } catch(IOException e) { e.printStackTrace(); }}if(osw ! =null) {try {
                osw.close();
            } catch(IOException e) { e.printStackTrace(); }}}}Copy the code

Code set

Common coding tables

  • ASCII: American standard information exchange code. Can be represented by seven bits of a byte.
  • Iso8859-1: Latin code watch. European code meters are represented by 8 bits of a byte.
  • GB2312: Chinese code table of China. Up to two bytes encode all characters
  • GBK: The Chinese code table has been upgraded to incorporate more Chinese characters. Two bytes encoding at most
  • Unicode: International standard code that combines all the characters currently used by humans. Assign a unique character code to each character. All text is represented by two bytes.
  • Utf-8: Variable length encoding that can represent a character in the range of 1 to 4 bytes.

  • A variety of transport-oriented UTF(UCS Transfer Format) standards emerged. As the name implies, UTF-8 is to transmit data 8 bits at a time, while UTF-16 is to transmit data 16 bits at a time. This is code designed for transmission and makes it borderless so that it can display characters from all cultures around the world.
  • Unicode simply defines a large, universal character set and assigns only a certain number to each character, depending on the character encoding scheme. The recommended Unicode encodings are UTF-8 and UTF-16.

Application code

  • Encoding: string –> byte array
  • Decode: byte array –> string
  • The encoding application of the transformation stream
    • Characters can be stored in a specified encoding format
    • Text data can be read in a specified encoding format
    • The action of specifying the encoding table is done by the constructor

Application requirements:

Client/browser side < — – > the background (Java, GO, Python, Node. Js, PHP) < — – > database

The character set used before and after must be uniform: UTF-8.

Standard input & output stream

System.in: The standard input stream, entered from the keyboard by default

System.out: Standard output stream, output from the console by default

The main method

The setIn(InputStream is) method of the System class respecifies the InputStream

The setOut(PrintStream ps) method of the System class respecifies the output stream.

practice

Typing a string from the keyboard requires that the entire line of the string read be converted to uppercase output. And then continue with the input operation,

Exit the program until “e” or “exit” is entered.

Design ideas

Method one: Implement Scanner and call next() to return a string

Method 2: Use system. in. System.in –> Transform stream –> BufferedReader’s readLine()

import java.io.*;
import java.util.Scanner;

/ * * *@authorHappy heart lake *@date2020/6/13 so * * /
public class Test {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(true){
            String data = sc.nextLine();
            if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
                System.out.println("End of program");
                break; } System.out.println(data.toUpperCase()); }}}Copy the code

or

public static void main(String[] args) {
    BufferedReader br = null;
    try {
        InputStreamReader isr = new InputStreamReader(System.in);
        br = new BufferedReader(isr);

        while (true) {
            System.out.println("Please enter the string:");
            String data = br.readLine();
            if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
                System.out.println("End of program");
                break; } String upperCase = data.toUpperCase(); System.out.println(upperCase); }}catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(br ! =null) {
            try {
                br.close();
            } catch(IOException e) { e.printStackTrace(); }}}}Copy the code

Printing flow

PrintStream and PrintWriter

Description:

  • Provides a series of overloaded print() and println() methods for output of a variety of data types
  • System.out returns an instance of PrintStream

Object flow

The ObjectOutputStream and ObjectInputStream

  • ObjectOutputStreamObjects in memory –> files in storage, sent out over the network: serialization process
  • ObjectInputStreamFiles in storage, received over the network –> objects in memory: deserialization process

Serialization of objects

The object serialization mechanism allows Java objects in memory to be converted into platform-independent binary streams, allowing such binary streams to be persisted on disk or transferred over the network to another network node. When other programs acquire the binary stream, they can revert to the original Java object.

The benefit of serialization is that any object that implements the Serializable interface can be converted to byte data, which can be restored when saved and transferred.

Serialization is the mechanism by which both arguments and return values of the RMI(Remote Method invoke-remote Method call) procedure must be implemented, which is the foundation of JavaEE. So serialization is the foundation of the JavaEE platform.

If you want an object to support serialization, you must make the class to which the object belongs and its properties serializable. In order for a class to be serializable, the class must implement one of the following two interfaces. Otherwise, NotserializableEXception is thrown


Classes that implement the Serializable interface have a static variable representing the serialized version identifier:

  • private static final long serialVersionUID

  • SerialVersionUID is used to indicate compatibility between different versions of a class. In short, the purpose is to versionize the serialized objects and determine whether the versions are compatible when deserializing them

  • If the class does not explicitly define this static constant, its value is automatically generated by the Java runtime environment based on the internal details of the class. If the instance variable of the class is modified, the serialVersionUID may change. Therefore, an explicit declaration is recommended.

  • Simply put, Java’s serialization mechanism verifies version consistency by determining the serialversionUID of a class at run time. When deserializing, the JVM compares the serialversionUID in the incoming byte stream with the serialversionUID of the corresponding local entity class. If they are the same, the serialversionUID is considered consistent and can be deserialized; otherwise, an exception will occur that the serialversionUID is inconsistent. (InvalidCastException)

The class of the object to be serialized needs to be met

  1. Interface required: Serializable (Identifying interface)
  2. The current class provides a global constant: serialVersionUID (sequence version number)
  3. In addition to the current Person class that needs to implement the Serializable interface, it must also ensure that its internal attributes are Serializable. (Base data types are serializable by default)

ObjectOutputStream and ObjectInputStream cannot serialize static and transient member variables

Serialization code implementation

Serialization: Write an object to disk or transfer it over the network

The serialized object must be serialized

@Test
public void testObjectOutputStream(a){
    ObjectOutputStream oos = null;

    try {
        //1. Create an object and create a stream
        oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
        / / 2. The operation flow
        oos.writeObject(new String("I love Tiananmen Square in Beijing"));
        oos.flush();// Refresh operation

        oos.writeObject(new Person("Johnny".23));
        oos.flush();

        oos.writeObject(new Person("Zhang Xueliang".23.1001.new Account(5000)));
        oos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(oos ! =null) {/ / 3. Close the flow
            try {
                oos.close();
            } catch(IOException e) { e.printStackTrace(); }}}}Copy the code

Deserialization code implementation

Deserialization: Read out the object data source of the disk

@Test
public void testObjectInputStream(a){
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream("object.dat"));

        Object obj = ois.readObject();
        String str = (String) obj;

        Person p = (Person) ois.readObject();
        Person p1 = (Person) ois.readObject();

        System.out.println(str);
        System.out.println(p);
        System.out.println(p1);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        if(ois ! =null) {try {
                ois.close();
            } catch(IOException e) { e.printStackTrace(); }}}}Copy the code