Using BufferedReader to read Text File

前端 未结 9 1478
你的背包
你的背包 2020-11-29 04:29

I\'m having problems with using the BufferedReader

I want to print the 6 lines of a text file:

public class Reader {

public static void main(String[         


        
9条回答
  •  一生所求
    2020-11-29 05:10

    Use try with resources. this will automatically close the resources.

    try (BufferedReader br = new BufferedReader(new FileReader("C:/test.txt"))) {
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (Exception e) {
    
    }
    

提交回复
热议问题