JAVA基础 | IO四个容易忽视的坑


前言

我们在使用IO的时候,一不小心就会落入坑中,我们该如何解决?


提示:以下是本篇文章正文内容,下面案例可供参考

第一坑:文件读写需要确保字符编码一致

场景:我们模拟将编码格式为GBK的值写入outputStream中,在使用UTF-8的格式将他输出。

private void ByteArrayOutputStreamTest(){
    byte[] bytes;
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()){

        byteArrayOutputStream.write("you ".getBytes());
        byteArrayOutputStream.write("see ".getBytes());
        byteArrayOutputStream.write("see 你 ".getBytes()));

        bytes = byteArrayOutputStream.toByteArray();

        System.out.println(new String(bytes, Charset.forName("GBK")));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

输出结果:
在这里插入图片描述

我们可以跟一下他的源码,发现他是有一个默认值的,默认的格式为UTF-8

public static Charset defaultCharset() {
    if (defaultCharset == null) {
        synchronized (Charset.class) {
            String csn = AccessController.doPrivileged(
                new GetPropertyAction("file.encoding"));
            Charset cs = lookup(csn);
            if (cs != null)
                defaultCharset = cs;
            else
                defaultCharset = forName("UTF-8");
        }
    }
    return defaultCharset;
}

我们再将他的字符编码一致都,再看下输出结果
在这里插入图片描述

第二坑:Files类调用readAllLines,会造成OOM

直接看源码,就知道为什么,一次将所有文件中的内容都读出,要是文件内容过大就会爆了。

public static List<String> readAllLines(Path path, Charset cs) throws IOException {
    //使用BufferedReader读取文件
    try (BufferedReader reader = newBufferedReader(path, cs)) {
        //声明一个List
        List<String> result = new ArrayList<>();
        //将读出的值,塞入list
        for (;;) {
            String line = reader.readLine();
            if (line == null)
                break;
            result.add(line);
        }
        return result;
    }
}

解决方案:
可以调用limit来限制需要读出的数据

Files.lines(Paths.get("read1.txt")).limit(2000);

第三坑:使用FIles类静态方法进行文件操作注意释放文件句柄

private static void wrong() {
    //ps aux | grep CommonMistakesApplication
    //lsof -p 63937
    LongAdder longAdder = new LongAdder();
    IntStream.rangeClosed(1, 1000000).forEach(i -> {
        try {
            Files.lines(Paths.get("demo.txt")).forEach(line -> longAdder.increment());
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
    log.info("total : {}", longAdder.longValue());
}

输出结果:
在这里插入图片描述
为什么会造成这个问题呢
主要是因为lines方法返回的是stream,句柄没有释放。
利用try-with-resources来解决这个问题,在结束的时候自动去关闭句柄

第四坑:注意读写文件要考虑设置缓冲区

在进行文件操作的时候,很少时候字节是使用字节流来读写文件,主要是字节流慢。
更多时候利用缓存区,进行批量的操作,减少传输的消耗,提高速度。

//操作字节流
private static void perByteOperation() throws IOException {
    Files.deleteIfExists(Paths.get("dest.txt"));

    try (FileInputStream fileInputStream = new FileInputStream("src.txt");
         FileOutputStream fileOutputStream = new FileOutputStream("dest.txt")) {
        int i;
        while ((i = fileInputStream.read()) != -1) {
            fileOutputStream.write(i);
        }
    }
}

//添加缓冲区
private static void bufferOperationWith100Buffer() throws IOException {
    Files.deleteIfExists(Paths.get("dest.txt"));

    try (FileInputStream fileInputStream = new FileInputStream("src.txt");
         FileOutputStream fileOutputStream = new FileOutputStream("dest.txt")) {
        byte[] buffer = new byte[100];
        int len = 0;
        while ((len = fileInputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }
    }
}
//操作缓冲字节流
private static void bufferedStreamBufferOperation() throws IOException {
    Files.deleteIfExists(Paths.get("dest.txt"));

    try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("src.txt"));
         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("dest.txt"))) {
        byte[] buffer = new byte[8192];
        int len = 0;
        while ((len = bufferedInputStream.read(buffer)) != -1) {
            bufferedOutputStream.write(buffer, 0, len);
        }
    }
}

总结

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值