我写了以下代码,将4000字节的0写入文件test.txt。然后,我一次读取相同的文件,大小写为1000字节。
FileOutputStream output = new FileOutputStream("test.txt");
ObjectOutputStream stream = new ObjectOutputStream(output);
byte[] bytes = new byte[4000];
stream.write(bytes);
stream.close();
FileInputStream input = new FileInputStream("test.txt");
ObjectInputStream s = new ObjectInputStream(input);
byte[] buffer = new byte[1000];
int read = s.read(buffer);
while (read > 0) {
System.out.println("Read " + read);
read = s.read(buffer);
}
s.close();
我期望发生的是读取1000字节四次。
Read 1000
Read 1000
Read 1000
Read 1000
然而,实际发生的是每1024字节我似乎得到“暂停”(缺乏一个更好的词)。
Read 1000
Read 24
Read 1000
Read 24
Read 1000
Read 24
Read 928
如果我尝试读取超过1024个字节,那么我将被限制在1024个字节。如果我尝试读取不到1024个字节,我仍然需要暂停1024字节的标记。
在十六进制检查输出文件test.txt后,我注意到,尽管事实上我只写了0个文件,但是有一个5个非零字节7A 00 00 04 00 1029字节的序列。 Here is the output from my hex editor.(太长时间不适合这个问题)
所以我的问题是:为什么当我完全写入0时,这五个字节出现在我的文件中?这5个字节与每1024个字节发生的暂停有关吗?为什么这是必要的?