首页 > 其他 > 详细

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

时间:2019-01-13 01:08:25      阅读:158      评论:0      收藏:0      [点我收藏+]
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接口。

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

原文:https://www.cnblogs.com/chichung/p/10261655.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!