首页 > 技术文章 > java - 正确关闭流

500m 2020-10-21 19:00 原文

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(); } } }

 

推荐阅读