After the nanny series, it's so simple, so straight

catalogue

1, What's going on at the bottom of IO?

2, Sort out the structure of classes

3, IO big point

4, Demonstration of incoming wave examples

Summary:

Click like and watch again, form the habit of praise, search on wechat [coriander Chat Game] and pay attention to me.

preface:

Some big guys in the group said they wanted me to write a NIO, but they never wrote it, but chatting with colleagues also said they were not very clear about Java io. Therefore, write java IO today, lay a foundation first, write NIO next time, let's start

1, What's going on at the bottom of IO?

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. There is an 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.

In order to ensure the stability and security of the operating system, the address space of a process is divided into User space and Kernel 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 help complete them. Therefore, if the user process wants to perform IO operations, it must indirectly access the Kernel space through system calls

2, Sort out the structure of classes

java's io is too complex for novices to master, because it's hard for novices to see the essence of the problem from the whole. The chat screenshots of my blacksmith friends and I can help you answer some questions.

The class structure is as follows

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.

3, IO big point

4, 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 complete");
  }
}

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(newFileInputStream(inFilePath));
            BufferedOutputStream bos = new BufferedOutputStream(newFileOutputStream(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:

Summary:

  • 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.

  • The implementation of Buffered and other buffers can avoid frequent disk reading and writing, and then improve IO processing efficiency.

  • Remember that the design mode of IO stream is decorator mode, and the function is upgraded by streaming.

  • Remember the three keywords stream, reader and buffered

Coriander is not easy to create, to praise, forward, to pay attention to three links, and to pay attention to my official account: "cilantro Chat Game" has more benefits.

Keywords: Java Programming Operating System Game Development

Added by EY on Wed, 09 Feb 2022 06:53:49 +0200