计算通过GZIPOutputStream形成的BufferedWriter写入文件的字节数 [英] Count the bytes written to file via BufferedWriter formed by GZIPOutputStream

查看:107
本文介绍了计算通过GZIPOutputStream形成的BufferedWriter写入文件的字节数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 BufferedWriter ,如下所示:

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
        new GZIPOutputStream( hdfs.create(filepath, true ))));

String line = "text";
writer.write(line);

我想查找写入文件的字节,而无需查询文件

I want to find out the bytes written to the file with out querying file like

hdfs = FileSystem.get( new URI( "hdfs://localhost:8020" ), configuration );

filepath = new Path("path");
hdfs.getFileStatus(filepath).getLen();

因为这会增加开销,但我不希望这样.

as it will add overhead and I don't want that.

我也不能这样做:

line.getBytes().length;

压缩前给出的大小.

推荐答案

您可以使用

You can use the CountingOutputStream from Apache commons IO library.

将其放在GZIPOutputStream和文件Outputstream(hdfs.create(..))之间.

Place it between the GZIPOutputStream and the file Outputstream (hdfs.create(..)).

将内容写入文件后,您可以从CountingOutputStream实例读取写入的字节数.

After writing the content to the file you can read the number of written bytes from the CountingOutputStream instance.

这篇关于计算通过GZIPOutputStream形成的BufferedWriter写入文件的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆