我正在运行以下代码:
public class SkipTest {
public static void main(String[] args) throws IOException {
FileOutputStream fileout = new FileOutputStream("./foo");
DataOutputStream output = new DataOutputStream( fileout );
output.writeLong(12345678L);
output.writeLong(87654321L);
output.writeInt(1234);
output.flush();
output.close();
FileInputStream input = new FileInputStream("./foo");
DataInputStream datain = new DataInputStream(input);
System.out.println(datain.readLong());
System.out.println(datain.readLong());
long skipped = datain.skip(8);
System.out.printf("Attempting to skip 8 bytes, actually skipped %d.\n", skipped);
datain.close();
}
}
根据文档,返回值.skip()
是实际跳过的字节数,可能小于请求的数量。我已经验证该文件在外部只有 20 个字节,但是当我运行上面的代码时,我得到以下输出:
12345678
87654321
Attempting to skip 8 bytes, actually skipped 8.
那么这是一个错误还是我做错了什么?当文件中只剩下 4 个字节时,它如何跳过 8 个字节?