Take you to deeply understand what Java IO is

Time:2021-9-23

Summary:The operating system is the housekeeper, and the computer equipment is the resource. If the process wants to operate the resource first, it must make a system call, have the operating system to process it, and then return it to the process. Is this kind of agent mode very common? Therefore, app is the program you write, the resource is the hard disk or other devices, and IO is the system call.

This article is shared from Huawei cloud community《Stunned, it turns out that Java IO is so simple》Original author: coriander Chat Game.

The operating system is the housekeeper, and the computer equipment is the resource. If the process wants to operate the resource first, it must make a system call, have the operating system to process it, and then return it to the process. Is this kind of agent mode very common? Therefore, app is the program you write, the resource is the hard disk or other devices, and IO is the system call.
Take you to deeply understand what Java IO is

In order to ensure the stability and security of the operating system, the address space of a process is divided intoUser spaceandKernel space。 For example, the applications we normally run run run in user space. Only the kernel space can carry out resource related operations at the system state level, such as file management, process communication, memory management and so on. In other words, if we want to perform IO operations, we must rely on the ability of kernel space. Moreover, programs in user space cannot directly access kernel space. When you want to perform IO operations, because you do not have permission to perform these operations, you can only initiate a system call and request the operating system to complete them. Therefore, if the user process wants to perform IO operations, it must passsystem callTo indirectly access kernel space

Java’s IO is too complex for novices to master, because it is difficult for novices to see the essence of the problem from the whole because they are only in this mountain. The chat screenshots of my blacksmith friends and I can help you answer some questions.
Take you to deeply understand what Java IO is

The class structure is as follows
Take you to deeply understand what Java IO is

When reading and writing files, you can first use the basic stream, then see whether you need the character stream, and finally use the stream with buffer. The design idea of IO stream is decorator mode, which can upgrade layer by layer.

IO big point

Take you to deeply understand what Java IO is

Demonstration of incoming wave examples

1. Access operation files (FileInputStream / FileReader, fileoutputstream / filewriter)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
*Copy file
*@ author coriander
*/
public class CopyFileWithStream {
   public static void main(String[] args) {
       int b = 0;
       String inFilePath = "D:\\wechat\\A.txt";
       String outFilePath = "D:\\wechat\\B.txt";
       try (FileInputStream in = new FileInputStream(inFilePath); FileOutputStream out = new FileOutputStream(outFilePath)) {
           while ((b = in.read()) != -1) {
               out.write(b);
          }
      } catch (IOException e) {
           e.printStackTrace();
      }
       System.out.println ("file copy completed");
  }
}

2. Use of cache stream (bufferedinputstream / bufferedoutputstream,

BufferedReader/BufferedWriter)

package org.pdool.iodoc;

import java.io.*;

/**
*Copy file
*
*@ author coriander
*/
public class CopyFileWithBuffer {
   public static void main(String[] args) throws Exception {
       String inFilePath = "D:\\wechat\\A.txt";
       String outFilePath = "D:\\wechat\\B.txt";
       try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inFilePath));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFilePath))) {
           byte[] b = new byte[1024];
           int off = 0;
           while ((off = bis.read(b)) > 0) {
               bos.write(b, 0, off);
          }
      }
  }
}

3. Get keyboard input

import java.util.Scanner;

public class TestScanner {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       while (scanner.hasNextLine()){
           System.out.println(scanner.nextLine());
      }
  }
}

Let’s look at the source code:
Take you to deeply understand what Java IO is

Summary:

  • The reader / writer is used to operate characters and add functions such as character encoding and decoding. It is suitable for reading or writing text information from files. In essence, the computer operates bytes. Whether it is network communication or file reading, reader / writer is equivalent to building a bridge between application logic and original data.
  • Buffered and other buffer implementations,Frequent disk reads and writes can be avoidedTo improve IO processing efficiency.
  • Remember that the design mode of IO flow is decorator mode, and the function is upgraded by streaming.
  • Remember the three keywords stream, reader and buffered

Click focus to learn about Huawei cloud’s new technologies for the first time~