The simplest IO understanding

Keywords: Java IntelliJ IDEA intellij-idea

IO stream

First, we should understand external memory, memory and cache

io is divided into input and output:

Personal understanding:

  • Output stream: output the file from the idea

  • Input stream: read the file to the idea

Input and output can be divided into:

  1. Byte stream: the smallest data unit in a data stream is bytes

    Output: OutputStream

    Input: InutStream

  2. Character stream: the smallest data unit in the data stream is a character. Characters in Java are encoded in Unicode. A character occupies two bytes (two bytes in both Chinese and English).

    Output: Writer

    Input: Reader

The most basic input and output:

System.out standard output stream

System.in standard input stream

You should be familiar with this. If you are not familiar with it, you should reflect on it

File class

Managing disk files and directories exclusively: creating abstract paths

Note: the File class can only manipulate the attributes of the File, but not the contents of the File.

  • Because the separator of each system path is different, java provides the separator method
//Do not use the separator field provided by Java. Note: this writing is only valid on Windows platforms
File f1 = new File("D:\\IO\\a.txt");Or D:/IO/a.txt
//Use the separator provided by Java File f2 = new 		     File("D:"+File.separator+"IO"+File.separator+"a.txt");
File Common methods of class

  ①,**Create method**

    1.boolean createNewFile() No return true Presence return false
    2.boolean mkdir() Create a directory. If the upper level directory does not exist, the creation will fail
    3.boolean mkdirs() Create a multi-level directory. If the upper level directory does not exist, it will be created automatically

  ②,**Delete method**

    1.boolean delete() Delete a file or directory. If it represents a directory, the directory must be empty to delete
    2.boolean deleteOnExit() Delete file after use

  ③,**Judgment method**

    1.boolean canExecute()Determine whether the file is executable
    2.boolean canRead()Determine whether the file is readable
    3.boolean canWrite() Determine whether the file is writable
    4.boolean exists() Determine whether the file or directory exists
    5.boolean isDirectory()  Determine whether this path is a directory
    6.boolean isFile()  Determine whether it is a file
    7.boolean isHidden()  Determine whether it is a hidden file
    8.boolean isAbsolute()Judge whether it is an absolute path. You can also judge if the file does not exist

  ④,**Acquisition method**

    1.String getName() Gets the name of the file or directory represented by this path
    2.String getPath() Convert this pathname to a pathname string
    3.String getAbsolutePath() Returns the absolute form of this abstract pathname
    4.String getParent()//null if there is no parent directory
...........

I don't like to write these object methods because you can find them in the jdk

Byte stream and character stream are directly introduced here. It's too simple. You can use it by looking at the api

  1. Read data stream
  2. Load into an array
  3. The traversal is stored in a file until - 1 is encountered

All that's left is method calls

Buffered or piped flow

Byte buffer stream: BufferedInputStream, BufferedOutputStream

Character buffer stream: BufferedReader, BufferedWriter

The so-called buffer stream is actually similar to our ordinary byte character stream. When you use the ordinary stream, you have loaded the data into the array, and we can see from the buffer stream source code that it has provided us with an array of size 8192;

//Byte buffered input stream
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream("io"+File.separator+"a.txt"));
        //Define a byte array to store data
        byte[] buffer = new byte[1024];
        int len = -1;//Defines an integer representing the number of bytes read
        while((len=bis.read(buffer))!=-1){
            System.out.println(new String(buffer,0,len));
        }
        //Close stream resource
        bis.close();<br><br>
         
        //Byte buffered output stream
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("io"+File.separator+"a.txt"));
        bos.write("ABCD".getBytes());
        bos.close();
//Character buffered input stream
        BufferedReader br = new BufferedReader(
                new FileReader("io"+File.separator+"a.txt"));
        char[] buffer = new char[10];
        int len = -1;
        while((len=br.read(buffer))!=-1){
            System.out.println(new String(buffer,0,len));
        }
        br.close();
         
        //Character buffered output stream
        BufferedWriter bw = new BufferedWriter(
                new FileWriter("io"+File.separator+"a.txt"));
        bw.write("ABCD");
        bw.close();

Posted by BinaryDragon on Fri, 03 Dec 2021 11:47:35 -0800