Java uses IO stream to read files and display them to console 2

Time:2020-8-1
package com.io.exam;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 *On behalf of the bank
 * 
 */
public class TextFileExamLine {
    /*
     *Choose to do: improve the program, after reading the file content, add the line number at the beginning of each line, and then output to the screen together with the content.
     *Tip: the read char array can be converted into StringBuilder, and then search for "\ n" in the string and insert the line number after it.
     */
    public static void main(String[] args) {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            FileReader = new FileReader ("read file path");
            bufferedReader = new BufferedReader(fileReader);

            String str = null;
            int row = 1;
            while ((str = bufferedReader.readLine()) != null) {
                System.out.println(row++ + "." + str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}