JDK1.7新特性-try-with-resources语句

    xiaoxiao2021-12-13  24

    先了解jdk1.6及之前版本IO流异常处理标准代码

    jdk1.7 IO流异常处理标准代码

    public void demo() throws FileNotFoundException, IOException { try( // 只有实现了AutoCloseable接口的IO类,才能将获得资源的代码写在小括号中。 // FileInputStream、FileOutputStream都已经实现了这个接口。 // 写在小括号中的IO类,jvm会自动帮你关闭流。 FileInputStream fis = new FileInputStream("a.txt"); FileOutputStream fos = new FileOutputStream("b.txt"); ){ byte[] arr = new byte[1024*8]; int len; while((len = fis.read(arr)) != -1) { fos.write(arr,0,len); } } }
    转载请注明原文地址: https://ju.6miu.com/read-950340.html

    最新回复(0)