【发布时间】:2012-08-17 19:19:16
【问题描述】:
我使用 this * discussion 中的代码来计算java 中文件的校验和。
我对这个工作有点困惑我在我的问题中应用它如下:
我有一个包含一些数据的文件。我已经使用
计算了文件中文本的大小 System.out.println(file1content.toString().getBytes().length); the o/p is 4096 bytes
当我尝试执行校验和代码时,我意识到正在读取的字节数是 4096+12 字节,这 12 字节是否等于文件名?
我有另一个文件 2,其内容与文件 1 相同(我肯定知道这一点,因为我将文本提取为字符串并将其与 String.equals 进行比较),但生成的校验和不同。我想知道为什么会这样?
我在这里错过了什么吗?
编辑1:
我正在使用以下循环从文件中读取数据:
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1024];
do {
numRead = fis.read(buffer);
System.out.println(" "+ numRead);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
numread 的输出是:
1024
1024
1024
1024
12
-1
问候, 八卦
【问题讨论】:
-
你怎么知道字节数多出12?
-
@erickson 我将使用此信息编辑问题