java获取文件的字节数,获取Java InputStream后面文件的字节数

博客讨论了在不消耗整个输入流的情况下,无法直接获取InputStream的字节数。虽然有些实现可能提供`available()`方法,但其并不保证返回总字节数。建议如果InputStream关联的是文件,可以尝试使用不同的API来获取文件大小。
摘要由CSDN通过智能技术生成

As the title says, I need to know how many bytes the file has that's "behind" an InputStream.

I don't want to download all bytes and count (takes to long). I just need to know how many bytes the file has.

Like this:

int numberOfBytes = countBytes(inputStream);

So, I need an implementation for countBytes(InputStream inputStream)

解决方案

Other than by consuming the entire stream and counting the bytes, you can't (there's no API for it).

There's the available() method, but it quite explicitly doesn't do what you're asking:

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not.

If the InputStream is associated with a file (and not, say, a socket), perhaps you could use a different API to get its size?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值