code学习

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