把代码写进try()括号里的作用

try (
        InputStream fis = new FileInputStream(source);
        OutputStream fos = new FileOutputStream(target)
     ){
        byte[] buf = new byte[8192];
  
        int i;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }

try括号内的资源会在try语句结束后自动释放,前提是这些可关闭的资源必须实现 java.lang.AutoCloseable 接口。InputStream 和OutputStream 父类中一定实现了AutoCloseable接口

你可能感兴趣的:(JavaSE)