java - 正确关闭流

package stream;

import java.io.*;

public class FileReaderTest {

    public static void main(String[] args) {
        
        File f = new File("src/stream","hello.txt");
      
     // 把流定义在try()里,try,catch或者finally结束的时候,会自动关闭
try(FileReader fr = new FileReader(f)){ char[] c = new char[(int)f.length()]; fr.read(c); System.out.println(c); }catch(IOException e){ e.printStackTrace(); } } }

 

posted @ 2020-10-21 19:00  武卡卡  阅读(593)  评论(0编辑  收藏  举报