Abstract:The operating system is the manager, and the computer’s equipment is the resource. If the process wants to operate on the resource first, it has to make a system call, and the operating system handles it, and then returns it to the process, is this kind of proxy pattern common? So apps are the programs you write, resources are hard drives or other devices, and IO is the system calls you make.

This article is shared from the Huawei cloud community “shocked, the original Javaio is so simple”, the original author: cilanander chat game.

The operating system is the manager, and the computer’s equipment is the resource. If the process wants to operate on the resource first, it has to make a system call, and the operating system handles it, and then returns it to the process, is this kind of proxy pattern common? So apps are the programs you write, resources are hard drives or other devices, and IO is the system calls you make.

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. As we normally run applications are running in user space, only the kernel space can carry out system-level resources related operations, such as file management, process communication, memory management, and so on. In other words, if we want to do IO operations, we must depend on the kernel space capability. Also, user-space programs do not have direct access to kernel space. When you want to perform IO operations, because you don’t have the permission to perform these operations, you can only make a system call and ask the operating system to help you complete them. Therefore, if a user process wants to perform an IO operation, it must indirectly access kernel space through a system call

Java IO is too complex, often novice is difficult to master, because only the edge of the body in the mountain, novice is often difficult to see the nature of the problem from all, I and iron friends chat screenshots can help you answer some.

The class structure is as follows

For normal file reading and writing, you can start with a base stream, then see if you need a character stream, and then use a stream with a buffer. The IO stream is designed with the decorator pattern, which allows you to upgrade functionality layer by layer.

IO class big soldier

To wave example display

1, access files (FileInputStream/FileReader, FileOutputStream/FileWriter)

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; Public class CopyFileWithStream {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 caching flow (BufferedInputStream/BufferedOutputStream,

BufferedReader/BufferedWriter) package org. Pdool. Iodoc; import java.io.*; Public class CopyFileWithBuffer {public static void main(String[] args) throws Exception {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 take a look at the source code:

Conclusion:

  • Reader/Writer, on the other hand, is used for manipulating characters, adding the ability to encode and decode characters, and is suitable for reading or writing text information from a file. Computers essentially operate on bytes, whether it’s network communication or file reading, and Reader/Writer Bridges the gap between application logic and raw data.
  • Buffered and other implementations with buffers can avoid frequent disk reads and writes, thus improving IO processing efficiency.
  • Remember that the IO stream is designed in decorator mode to upgrade functionality.
  • Remember stream, reader, and buffered

Click on the attention, the first time to understand Huawei cloud fresh technology ~