IO File Operations in java: Reading Output by Line


IO File Operation in JavaIt is a very important part of java. In the written examination of financial securities companies, it often encounters the operation of files. Today’s article is about simple text files read by line and output to the console.

package com.lsl.readAndwrite;


public class ReadAndOut {
    public static void main(String[] args) throws IOException {
         * 1. Open a file
         * 2. New an InputStream Reader_FileInputStream_file
         * 3. Read with BufferedReader (InputStreamReader) readLine () method
         * */
        File f = new File("D:\book\1.txt");
        InputStreamReader readf = new InputStreamReader(new FileInputStream(f),"GBK");
       // There is a possibility of scrambling
        BufferedReader bfReader = new BufferedReader(readf);
        String txtline = null;
        while ((txtline = bfReader.readLine()) != null) {

Results Screenshot:
IO File Operations in java: Reading Output by Line

Scrambling problem:

FileReader gets characters, so bytes must have been recognized as characters by some kind of encoding.
The encoding method used by FileReader is the return value of Charset. defaultCharset (), and if it is a Chinese operating system, it is GBK.
FileReader can't set the encoding mode manually. In order to use other encoding methods, InputStreamReader can only be used instead.
Read with InputStreamReader