我从网站下载文件并检查大小(与我检查操作系统中的大小(以字节为单位)相同)。
connection.getContentLength();
int sizeBefore = connection.getContentLength();
BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream());
File destFile = new File(destFileName);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(destFile));
while ((i = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(i);
}
long sizeAfter = destFile.length();
bufferedOutputStream.flush();
bufferedInputStream.close();
if (sizeAfter == sizeBefore) {
log.debug("Downloaded file correct");
}
然后我也尝试通过其他方式检查存储的文件(NIO):
long size = Files.size(destFile.toPath())));
结果与操作系统的大小不同。为什么?
最佳答案
二进制前缀:
http://en.wikipedia.org/wiki/Binary_prefix#Adoption_by_IEC_and_NIST
Windows 使用 1024 字节为 1 KB (2^10),而 Linux 则使用 1000 字节为 1 KB。这会以 MB、GB 等形式传播...
关于java 为什么文件大小随操作系统不同而不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27405581/