利用files类对文件进行读和写,里面的方法自带关闭流 (jdk7里的try-with-resouese)
try { byte[] bytes = Files.readAllBytes(Paths.get("E:\testAutoCloseable.txt")); String s = new String(bytes, StandardCharsets.UTF_8); Files.write(Paths.get("E:\testAutoCloseable1.txt"), s.getBytes()); } catch (IOException e) { e.printStackTrace(); }