inputstream示例
InputStream类skip()方法 (InputStream Class skip() method)
skip() method is available in java.io package.
skip()方法在java.io包中可用。
skip() method is used to skip the given number of bytes of data from this InputStream.
skip()方法用于从此InputStream跳过给定数量的数据字节。
skip() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
skip()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
skip() method may throw an exception at the time of skipping bytes data.
skip()方法在跳过字节数据时可能会引发异常。
IOException: This exception may throw when getting any input/output error while performing.
IOException :在执行过程中遇到任何输入/输出错误时,可能引发此异常。
Syntax:
句法:
public long skip(long number);
Parameter(s):
参数:
long number – represents the number of bytes to be skipped.
long number –表示要跳过的字节数。
Return value:
返回值:
The return type of the method is long, it returns the exact number of bytes skipped.
该方法的返回类型为long ,它返回跳过的确切字节数。
Example:
例:
// Java program to demonstrate the example
// of long skip(long number) method
// of InputStream
import java.io.*;
public class SkipOfIS {
public static void main(String[] args) throws Exception {
InputStream is_stm = null;
try {
// Instantiates FileInputStream
is_stm = new FileInputStream("D:\\includehelp.txt");
for (int val = 0; val < 8; ++val) {
// By using read() method is to read
// a byte from is_stm
is_stm.read();
// Display corresponding bytes value
byte b = (byte) val;
// Display value of b
System.out.println("is_stm.read(): " + b);
// By using skip() method is to skip
// 1 bytes data from the is_stm
long skip_byte = is_stm.skip(1);
System.out.println("is_stm.skip(1): " + skip_byte);
System.out.println();
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// with the help of this block is to
// free all necessary resources linked
// with the stream
if (is_stm != null) {
is_stm.close();
}
}
}
}
Output
输出量
is_stm.read(): 0
is_stm.skip(1): 1
is_stm.read(): 1
is_stm.skip(1): 1
is_stm.read(): 2
is_stm.skip(1): 1
is_stm.read(): 3
is_stm.skip(1): 1
is_stm.read(): 4
is_stm.skip(1): 1
is_stm.read(): 5
is_stm.skip(1): 1
is_stm.read(): 6
is_stm.skip(1): 1
is_stm.read(): 7
is_stm.skip(1): 1
翻译自: https://www.includehelp.com/java/inputstream-skip-method-with-example.aspx
inputstream示例