BufferedInputStream: Skipping Bytes in Java
The correct answer is 'B - skip(n)'.
The skip(n) method in Java's BufferedInputStream class allows you to skip a specified number of bytes (represented by 'n') in the input stream. This method is particularly useful for efficiently moving through large data files without reading the entire contents.
Example:
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file.txt"));
long bytesToSkip = 1024; // Skip the first 1024 bytes
bis.skip(bytesToSkip);
Key Points:
skip(n)returns the actual number of bytes skipped, which may be less than 'n' if the end of the stream is reached.- The method throws an
IOExceptionif an I/O error occurs during the operation.
Other Options:
- 'A - mark': The
mark(readlimit)method marks the current position in the stream, allowing you to return to that position later usingreset(). However, it doesn't directly skip bytes. - 'C - reset': The
reset()method restores the stream's position to the previously marked location. It doesn't skip bytes. - 'D - close': The
close()method closes the stream, releasing any resources associated with it. It doesn't skip bytes.
原文地址: https://www.cveoy.top/t/topic/oz13 著作权归作者所有。请勿转载和采集!