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接口。