I'm running the following code:
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();
}
}
According to the docs, the return value of .skip()
is the actual number of bytes skipped, which may be less than the requested number. I've verified that the file is only 20 bytes externally, but when I run the code above I get the following output:
12345678
87654321
Attempting to skip 8 bytes, actually skipped 8.
So is this a bug or am I doing something wrong? How can it skip 8 bytes when there are only 4 left in the file?