skipNBytes(long): This method is available in the java.io.BufferedInputStream class of Java.
Syntax:
void java.io.InputStream.skipNBytes(long n) throws IOException
This method takes one argument. This method skips over and discards exactly n bytes of data from this input stream.
Note: If n is zero, then no bytes are skipped.If n is negative, then no bytes are skipped.
Parameters: One parameter is required for this method.
n: the number of bytes to be skipped.
Throws:
1. EOFException - if end of the stream is encountered before the stream can be positioned n bytes beyond its position when this method was invoked.
2. IOException - if the stream cannot be positioned properly or if an I/O error occurs.
Approach 1: When no exception
Java
Output:
java.io.BufferedInputStream@182decdb
Approach 2: EOFException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamskipNBytes {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);long n = 2;bufferedInputStream.skipNBytes(n);System.out.println(bufferedInputStream);}}
Output:
Exception in thread "main" java.io.EOFException at java.base/java.io.InputStream.skipNBytes(InputStream.java:605)
Approach 3: IOException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamskipNBytes {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);long n = 2;bufferedInputStream.close();bufferedInputStream.skipNBytes(n);System.out.println(bufferedInputStream);}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:168) at java.base/java.io.BufferedInputStream.skip(BufferedInputStream.java:366) at java.base/java.io.InputStream.skipNBytes(InputStream.java:595)
No comments:
Post a Comment