Synchronous and asynchronous: Describes the message notification mechanism between processes

  • Synchronization: The current task must wait for the previous task to complete.

  • Asynchronous: The current task can execute the next task without waiting for the previous task to complete.

Blocking and non-blocking: Describes the behavior of threads while waiting

  • Block: While waiting, the current thread is blocked, waiting for other tasks to complete and wake up

  • Non-blocking: When waiting for other resources, the system keeps polling and waiting and does not enter sleep state

1. JavaIO flow classification

  • According to the flow direction, it can be divided into input flow and output flow.

  • According to operation unit, it can be divided into byte stream and character stream.

  • Flows are divided into node flows and processing flows according to their roles.

The Java I/O stream consists of more than 40 classes that look clutter-like but are actually very regular and closely related to each other. The Java I0 stream’s more than 40 classes are derived from the following four abstract base classes.

  • InputStream/Reader: The base class for all input streams, which are byte input streams and character input streams.
  • OutputStream/Writer: Base class for all output streams, byte output streams and character output streams.

2.IO

Can tell the classification and functions of I/O streams

Ability to write data to files using byte output streams

Ability to read data into a program using a byte input stream

Understand the principle of the read(byte[]) method for reading data

The ability to copy files using byte streams

Ability to write data to files using FileWirter

Be able to tell the difference between closing and refreshing methods in FileWriter

5 ways to write data using FileWriter

You can use FileWriter to write data for line breaks and appending

Ability to read data using FileReader

Ability to read data one character array at a time using FileReader

The configuration information in the file can be loaded using the load method of Properties

IO model: characters are read into the Linux buffer area first and then into the user thread area

1. I/O classification

According to the data flow: input flow, output flow

By data type: byte stream, character stream

The input stream The output stream
Byte stream Byte InputStream InputStream Byte OutputStream OutputStream
Characters of the flow The character input stream Reader Character output stream Writer

3. OutputStream/InputStream

(1) Common methods
  • Public void close() : Closes this output stream and releases any system resources associated with this stream.
  • Public void flush() : Flushes this output stream and forces any buffered output bytes to be written out.
  • Public void write(byte[] b) : writes b.length bytes from the specified byte array to the output stream.
  • Public void write(byte[] b, int off, int len) : Writes len bytes from the specified byte array, starting with offset off, to the output stream.
  • Public abstract void write(int b) : Outputs the specified byte stream.
FileOutputStream class

OutputStream subclass FileOutputStream FileOutputStream, used to write data to a file

  • A constructor

    Public FileOutputStream(File File) : Creates a File output stream to write to a File represented by the specified File object.

    Public FileOutputStream(String name) : creates a FileOutputStream and writes to the file with the specified name.

    public class FileOutputStreamConstructor throws IOException {
    	public static void main(String[] args) {
    	// Create a stream object with the File object
    	File file = new File("a.txt");
    	FileOutputStream fos = new FileOutputStream(file);
    	// Create a stream object with the file name
    	FileOutputStream fos = new FileOutputStream("b.txt"); }}Copy the code
  • Write byte stream

    The write(int b) method can write one byte of data at a time.

    File file = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT");
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(97); fos.close(); Output: aCopy the code

    Write (byte[] b), which writes out the data in the array each time.

    File file1 = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 2. TXT");
    FileOutputStream fos1 = new FileOutputStream(file1);
    byte[] b="Study hard.".getBytes(); fos1.write(b); fos1.close(); Output: Study hardCopy the code

    Write (byte[] b, int off, int len), write len bytes each time starting from the off index.

    // Create a stream object with the file name
    FileOutputStream fos = new FileOutputStream("fos.txt");
    // The string is converted to a byte array
    byte[] b = "abcde".getBytes();
    // Write from index 2, 2 bytes. Index 2 is C, two bytes, CD.
    fos.write(b,2.2);
    // Close the resourcefos.close(); Output: CDCopy the code
  • Data appending continuation

    Public FileOutputStream(File File, Boolean append) : Creates a File output stream to write to a File represented by the specified File object.

    Public FileOutputStream(String name, Boolean append) : creates a FileOutputStream and writes to the file with the specified name. For both constructors, a Boolean value is passed in as an argument, true to append data, false to empty the original data.

            File file1 = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 2. TXT");
            FileOutputStream fos1 = new FileOutputStream(file1,true);
            byte[] b1="Study hard.".getBytes();
            fos1.write(b1);
    
            File file2 = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 2. TXT");
            FileOutputStream fos2 = new FileOutputStream(file2,true);
            byte[] b2="Up every day".getBytes();
            fos1.write(b2);
            fos1.close();
            fos2.close();
    Copy the code
  • Write a newline

    On Windows, each line ends with a carriage return + line feed, i.e. \r\n;

    On Unix, each line ends with a newline, i.e. \n;

    On the Mac, each line ends with a carriage return, or \r. Mac OS X is unified with Linux.

    public class FOSWrite {
    	public static void main(String[] args) throws IOException {
    		// Create a stream object with the file name
    		FileOutputStream fos = new FileOutputStream("fos.txt");
    		// Define a byte array
    		byte[] words = {97.98.99.100.101};
    		// go through the number group
    		for (int i = 0; i < words.length; i++) {
    		// Write out a byte
    			fos.write(words[i]);
        	// Write out a newline, which is converted to an array
        	fos.write("\r\n".getBytes());
    		}
            // Close the resourcefos.close(); }}Copy the code

(3) the FileInputStream class

  • A constructor

    FileInputStream(File File) : Creates a FileInputStream by opening a connection to the actual File, named by the File object File in the File system.

    FileInputStream(String name) : Creates a FileInputStream by opening a connection to the actual file named by the pathname in the file system.

    File file = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT");
    FileInputStream fis1=new FileInputStream(file);
    FileInputStream fis2=new FileInputStream("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT");
    Copy the code
  • Read byte data

    The read() method reads bytes, one byte at a time, promoted to int, at the end of the file, returning -1

    public classInput byte stream{
        public static void main(String[] args) throws IOException {
            File file = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT");
            FileInputStream fis1=new FileInputStream(file);
            int b=0;
            while((b=fis1.read())! = -1){
                System.out.println((char)b); } System.out.println(b); fis1.close(); }}Copy the code

    Read (byte[] b) returns the number of valid bytes read each time b is read into the array. At the end of the read, -1 is returned

    public classInput byte stream{
        public static void main(String[] args) throws IOException {
            File file = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT");
            FileInputStream fis1=new FileInputStream(file);
            byte[] b=new byte[2];
            int len=0;
            while((len =fis1.read(b))! = -1){
                System.out.println(len);
                System.out.println(new String(b,0,len)); } fis1.close(); }}Copy the code

(4) Case copy picture

Copy an image to the destination address

public classInput byte stream{
    public static void main(String[] args) throws IOException {
        File fileInput = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ img JPG");
        FileInputStream fis=new FileInputStream(fileInput);
        FileOutputStream fos = new FileOutputStream("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ \ \ \ \ aa bb img_result. JPG");
        byte[] b=new byte[1024];
        int len=0;  
        while((len=fis.read(b))! = -1){
            fos.write(b,0,len); } fos.close(); fis.close(); }}Copy the code

3. The character stream Reader/Write

A character input stream Reader

Read and write data in character units, used specifically for processing text files

  • The common method

    Public void close() : Closes this stream and releases any system resources associated with it.

    Public int read() : Reads a character from the input stream.

    Public int read(char[] cbuf) : Reads some characters from the input stream and stores them in the character array cbuf.

(2) FileReader class

  • A constructor

    FileReader(File File) : Creates a new FileReader, given the File object to read.

    FileReader(String fileName) : Creates a new FileReader, given the name of the file to read.

    public class FileReaderConstructor throws IOException{
    	public static void main(String[] args) {
    		// Create a stream object with the File object
    		File file = new File("a.txt");
    		FileReader fr = new FileReader(file);
    		// Create a stream object with the file name
    		FileReader fr = new FileReader("b.txt"); }}Copy the code
  • Reading character data

    The read method, which reads data one character at a time, promotes it to an int, and reads to the end of the file, returning -1

    public classInput character stream{
        public static void main(String[] args) throws IOException {
            File file = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT");
            FileReader fir = new FileReader(file);
            
            int b=0;
            while((b=fir.read())! = -1){
                System.out.println((char)b); } fir.close(); }}Copy the code

    Read (char[] cbuf) returns the number of valid characters read each time b characters are read into the array. At the end of the read, -1 is returned

    public classInput character stream{
        public static void main(String[] args) throws IOException {
            File file = new File("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT");
            FileReader fir = new FileReader(file);
            char[] buf=new char[2];
            int b=0;
            while((b=fir.read(buf))! = -1){
                System.out.println(new String(buf,0,b)); } fir.close(); }}Copy the code

(3) Character output stream FileWrite

  • The common method

    Void write(int c) Writes a single character.

    Void write(char[] cbuf) Writes to the character array.

    Abstract void write(char[] cbuf, int off, int len) Write a part of the character array, the starting index of the array off, the number of characters written by len.

    Void write(String STR) Writes a String.

    Void write(String STR, int off, int len) A part of a String to be written to, the starting index of the String to be written by off, and the number of characters written by len.

    Void flush() flushes the buffer for this stream.

    Void close() closes the stream, but first refreshes it.

(4) FlieWriter class

  • A constructor

    FileWriter(File File) : Creates a new FileWriter, given the File object to read.

    FileWriter(String fileName) : Creates a new FileWriter, given the name of the file to read.

    public class FileWriterConstructor {
    	public static void main(String[] args) throws IOException {
    		// Create a stream object with the File object
    		File file = new File("a.txt");
    		FileWriter fw = new FileWriter(file);
    		// Create a stream object with the file name
    		FileWriter fw = new FileWriter("b.txt"); }}Copy the code
  • Write out character data

    The write(int b) method can write out one character at a time

    public classOutput character stream{
        public static void main(String[] args) throws IOException {
            FileWriter fiw = new FileWriter("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 2. TXT");
    
            fiw.write(97);
            fiw.write("a");
            fiw.write("Hello?");
            fiw.write(3000); fiw.close(); }}Copy the code

    write(char[] cbuf)

    Write (char[] cbuf, int off, int len). Write (char[] cbuf, int off, int len). Write (char[] cbuf, int off, int len)

    public classOutput character stream{
        public static void main(String[] args) throws IOException {
            FileWriter fiw = new FileWriter("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 2. TXT");
            char[] chars="Test write functionality to see if it works.".toCharArray();
            fiw.write(chars);
            fiw.write("\n");
            fiw.write(chars,0,chars.length); fiw.close(); }}Copy the code
  • Write a string

    Write (String STR) and write(String STR, int off, int len).

    public class FWWrite {
    	public static void main(String[] args) throws IOException {
    		// Create a stream object with the file name
    		FileWriter fw = new FileWriter("fw.txt");
    		/ / string
    		String msg = "Dark Horse Programmer";
    		// Write out the character array
    		fw.write(msg); // Dark horse programmer
    		// Write from index 2, 2 bytes. Index 2 is' procedure ', two bytes, which is' program '.
    		fw.write(msg,2.2); / / application
    		// Close the resourcefos.close(); }}Copy the code
  • Refresh and close

    Flush: Flushes the buffer so that the stream object can continue to be used. Close: Flush the buffer first and then inform the system to release the resource. The stream object can no longer be used.

    public class FWWrite {
    	public static void main(String[] args) throws IOException {
    		// Create a stream object with the file name
    		FileWriter fw = new FileWriter("fw.txt");
    		// Write the data, by flush
    		fw.write('brush'); // Write the first character
    		fw.flush();
    		fw.write('new'); // Continue to write the second character, write success
    		fw.flush();
    		// Write the data by close
    		fw.write('off'); // Write the first character
    		fw.close();
    		fw.write('closed'); Java.io. IOException: Stream closedfw.close(); }}Copy the code

    4. Buffer flow NIO

  • Byte buffer stream: BufferedInputStream, BufferedOutputStream

  • Character buffer streams: BufferedReader, BufferedWriter

The basic principle of buffer flow is that a built-in buffer array of default size is created when a stream object is created to reduce I/O times and improve read/write efficiency.

(I) byte buffer stream

  • A constructor

    Public BufferedInputStream(InputStream in) : creates a new BufferedInputStream.

    Public BufferedOutputStream(OutputStream out) : creates a new BufferedOutputStream.

    // Create a byte buffered input stream
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt"));
    // Create a byte buffered output stream
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
    Copy the code
  • Code demo

    import java.io.*;
    
    public classBuffer byte stream{
        public static void main(String[] args) throws IOException {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ img JPG"));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ \ \ \ \ aa bb img2. JPG"));
    
            int len=0;
            byte[] b=new byte[8*1024];
            while((len=bis.read(b))! = -1){
                bos.write(b,0,len); } bos.close(); bis.close(); }}Copy the code

(2) Character buffer stream

  • BufferedReader/BufferedWriter constructors

    Public BufferedReader(Reader in) : Creates a new buffered input stream.

    Public BufferedWriter(Writer out) : Creates a new buffered output stream.

  • Specific methods

    BufferedReader: public String readLine() : Reads a line of text.

    BufferedWriter: public void newLine() : Writes a line of delimiters defined by system properties.

     public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 2. TXT"));
            String line=null;
            while((line=br.readLine())! =null){
                System.out.println(line);
                System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
            }
    	    br.close();
     }
    
     public static void main(String[] args) throws IOException {
    
            BufferedWriter bw = new BufferedWriter(new FileWriter("C: \ \ Users \ \ YT \ \ Documents \ \ 2020 \ \ 01 Java basic grammar \ \ code \ \ aa \ \ 1. TXT"));
            bw.write("12345667".2.2);
            bw.newLine();
            bw.write("12345667".4.2);
            bw.newLine();
            bw.write("12345667".6.2);
    
            bw.close();
      }
    
    Copy the code