IO, file quick look-up table

1. Classification of flow:

1) Classification by direction: input stream, output stream

2) Classification by operation unit: byte stream   Character stream

3) Combination: byte input stream byte output stream character input stream   Character input stream

2. Byte input stream:

1. Abstract parent: InputStream ------ cannot be instantiated

2. Ordinary children:

FileInputStream ---- byte input stream of operation file

Construction method parameter: file   / String pathname

BufferedInputStraem ---- efficient byte input stream

Construction method parameter: InputStream, but the abstract parent object cannot be created, so FileIutputStream is passed

3. Byte output stream:

1. Abstract parent: OutputStream ------ cannot be instantiated

2. Ordinary children:

FileOutputStream ---- byte output stream of operation file

Construction method parameter: file   / String pathname

Note: there is a parameter boolean append by default, and the default value is false, that is, overwrite output

If the second parameter append of the FileOutputStream constructor is set to true, the effect of appending output will be achieved

BufferedOutputStraem ---- efficient byte output stream

Constructor parameter: OutputStream, but the abstract parent object cannot be created, so FileOutputStream is passed

4. Character input stream:

 

1. Abstract parent: Reader ------ cannot be instantiated

2. Ordinary children:

FileReader ---- character input stream of operation file

Construction method parameter: file   / String filename

BufferedReader ---- efficient character input stream

Construction method parameter: InputStream, but the abstract parent object cannot be created, so FileReader is passed

5. Character output stream:

1. Abstract parent: Writer ------ cannot be instantiated

2. Ordinary children:

FileWriter --- character output stream of operation file

Construction method parameter: file   / String filename

Note: there is a parameter boolean append by default, and the default value is false, that is, overwrite output

If the second parameter append of the FileWriter constructor is set to true, the output will be appended

BufferedOutputStraem ---- efficient byte output stream

BufferedWriter ---- efficient byte input stream

Constructor parameter: Writer, but cannot create abstract parent object, so FileReader is passed

package cn.tedu.file;

import java.io.File;

public class TestFile {
    public static void main(String[] args) {
        //1. Create the object of file class
        /**
         * ready Folders and 1.txt need to be created manually
         * File Manual package import is required: import java.io.File
         * The path pathname is String data and must be written correctly, or the file cannot be found
         * The complete file name consists of two parts: file name + suffix
         */
        File file=new File("D:\\ready");
        System.out.println(file.length());
        System.out.println(file.isDirectory());//false to judge that the current file object is a folder
        System.out.println(file.isFile()); //Determine whether the current is a folder
        System.out.println(file.getName());//Gets the name of the file itself
        System.out.println(file.getParent());//Get file parent path
        System.out.println(file.getAbsoluteFile());//Get absolute path with drive letter
        System.out.println(file.exists());//Gets whether the path specified by the current file object exists
    }
}
0
true
false
ready
D:\
D:\ready
true

Process finished with exit code 0

Operation input stream InputStream fileInputStream

package cn.tedu.file;
import java.io.*;

/**
 * This class is used to practice byte input streams
 */
public class TestIn {
    public static void main(String[] args) {
        method();//Read of byte input stream
    }

    //This method is used to test the reading of byte stream
    private static void method() {
      InputStream in = null;
        //1. Create a stream object. Note that InputStream is an abstract parent class and cannot be instantiated.
        try {
             in = new FileInputStream(new File("D:\\ready\\1.txt"));
            InputStream in2 = new FileInputStream("D:\\ready\\1.txt");
           //2. Use the stream object to read the data in the specified file
            // The read() method reads a byte every time it is called. If it reads the end of the file data, it returns - 1,
            // The return value of this method is int, and all the codes corresponding to the specified characters will be found and printed
//            System.out.println(in.read());
            //2.2 optimize the code and use circular reading
            //Define variables to save the data read this time
            int b;
            //Cycle to read data. As long as the read data is not equal to - 1, it indicates that there is still data. If the cycle conditions are met, continue the cycle
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace(); //Default writing method: print error message
        }finally {
            /*finally{}The code block is the third part of try catch. This part will be executed no matter whether exceptions are caught or not, so it is often used to close the flow*/
            //3. Release resources. The flow resources must be released when they are used up!
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

  Operation input stream efficient InputStream ButterInputStream

package cn.tedu.file;
import java.io.*;

/**
 * This class is used to practice byte input streams
 */
public class TestIn {
    public static void main(String[] args) {
//        method();// Read of byte input stream
        method2();//Efficient byte input stream reading

    }

    private static void method2() {
        InputStream in=null;
//        InputStream in=new BufferedInputStream(new FileInputStream(new File("D:\\ready")))
        try {
             in=new BufferedInputStream(new FileInputStream("D:\\ready\\1.txt"));
            //2. Using flow objects
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //This method is used to test the reading of byte stream
    private static void method() {
      InputStream in = null;
        //1. Create a stream object. Note that InputStream is an abstract parent class and cannot be instantiated.
        try {
             in = new FileInputStream(new File("D:\\ready\\1.txt"));
            InputStream in2 = new FileInputStream("D:\\ready\\1.txt");
           //2. Use the stream object to read the data in the specified file
            // The read() method reads a byte every time it is called. If it reads the end of the file data, it returns - 1,
            // The return value of this method is int, and all the codes corresponding to the specified characters will be found and printed
//            System.out.println(in.read());
            //2.2 optimize the code and use circular reading
            //Define variables to save the data read this time
            int b;
            //Cycle to read data. As long as the read data is not equal to - 1, it indicates that there is still data. If the cycle conditions are met, continue the cycle
            while((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (Exception e) {
            e.printStackTrace(); //Default writing method: print error message
        }finally {
            /*finally{}The code block is the third part of try catch. This part will be executed no matter whether exceptions are caught or not, so it is often used to close the flow*/
            //3. Release resources. The flow resources must be released when they are used up!
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
package cn.tedu.file;

import java.io.*;

/**
 * This class is used to practice character output stream writer
 */
public class TestOut2 {
    public static void main(String[] args) {
        method1();//Output stream for testing normal characters
//        method2();// Used to test efficient character output stream

    }

    private static void method2() {
        BufferedWriter out=null;
        try {
//            BufferedWriter out3 = new BufferedWriter(new FileWriter(new File("D:\\ready\\1.txt")));
//            BufferedWriter out2 = new BufferedWriter(new FileWriter("D:\\ready\\1.txt"));
//            BufferedWriter out1 = new BufferedWriter(new FileWriter(new File("D:\\ready\\1.txt"), true));
            out = new BufferedWriter(new FileWriter("D:\\ready\\1.txt", true));
        out.write(30);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method1() {
        FileWriter out = null;
        try {
            out = new FileWriter(new File("D:\\ready\\1.txt"), true);
//            out=new FileWriter ("D:\\ready\\1.txt",true);
            out.write(68);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Tags: Java Back-end

Posted on Thu, 21 Oct 2021 04:01:35 -0400 by bassdog65