码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-13 02:03:07      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:div   close   fileinput   stat   string   为什么   code   NPU   while   

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版本

标签:div   close   fileinput   stat   string   为什么   code   NPU   while   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!