利用files类对文件进行读和写,里面的方法自带关闭流 (jdk7里的try-with-resouese)

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。 文件读取和写出 随笔 第1张
 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();
    }
View Code

 

文件读取和写出 随笔 第3张

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄