自动关闭IO流-jdk1.7版本

public static void main(String[] args) throws IOException {
        try(
            FileInputStream fis = new FileInputStream("xxx.txt");
            FileOutputStream fos = new FileOutputStream("yyy.txt");
        ){
            int b;
            while ((b = fis.read()) != -1){
                fos.write(b);
            }
        }
    }

try(...){...}

为什么可以呢?

因为IO流的类实现了AutoCloseable接口。

猜你喜欢

转载自www.cnblogs.com/chichung/p/10261655.html