Close file in finally block doesn't work

后端 未结 3 563
不思量自难忘°
不思量自难忘° 2021-02-07 11:53
try {
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);
    String line = null;
} catch (FileNotFoundException fnf) {
    fnf.pri         


        
3条回答
  •  被撕碎了的回忆
    2021-02-07 12:06

    Now finally block is not needed,

    try (FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);){
    
        String line = null;
    
        }
    
    } catch(FileNotFoundException fnf) {
        fnf.printStackTrace();
    } 
    

    now automatically close your readers

提交回复
热议问题