skip(long): This method is available in the java.io.BufferedInputStream class of Java.
Syntax:
long java.io.BufferedInputStream.skip(long n) throws IOException
This method takes one argument. This method tries to skip the n number of bytes.
Parameters: One parameter is required for this method.
n: the number of bytes to be skipped.
Returns: the actual number of bytes skipped.
Throws:
IOException - if this input stream has been closed by invoking its close() method, in.skip(n) throws an IOException or an I/O error occurs.
Approach 1: When no exception
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamskip {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);long n = 2;System.out.println(bufferedInputStream.skip(n));}}
Output:
0
Approach 2: IOException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamskip {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);long n = 2;bufferedInputStream.close();System.out.println(bufferedInputStream.skip(n));}}
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)
No comments:
Post a Comment