正如标题所述,我需要知道InputStream“后面”的文件有多少字节。
我不想下载所有字节然后计算(太耗时间)。我只需要知道文件有多少字节。
像这样:
int numberOfBytes = countBytes(inputStream);
因此,我需要一个实现 countBytes(InputStream inputStream) 的方法。
正如标题所述,我需要知道InputStream“后面”的文件有多少字节。
我不想下载所有字节然后计算(太耗时间)。我只需要知道文件有多少字节。
像这样:
int numberOfBytes = countBytes(inputStream);
因此,我需要一个实现 countBytes(InputStream inputStream) 的方法。
available()
方法,但是它明确不会返回您要求的内容:skip()
方法来近似计算文件的大小吗?int bytes = 1024; // Chunk size for skipping. Adjust as necessary
try {
int skipped = 0;
while(stream.available()) {
stream.skip(bytes);
skipped += bytes;
// Elided...do something with skipped
}
} catch(IOException ex) {
// Handle a skip that's too big
}
IOException
,但这是留给读者的练习。