Java FilterInputStream.skip()
方法
原文:https://www.studytonight.com/java-file-io/java-filterinputstream-skip-method
在本教程中,我们将学习 Java 中 FilterInputStream 类的skip()
方法。此方法用于跳过并丢弃当前FilterInputStream
中的给定字节数。这是一种非静态方法,可在 java.io 包中获得。
句法
这是此方法的语法声明。它将“n”个要跳过的字节数作为参数,并返回跳过的字节总数。
public long skip(long n) throws IOException
示例:跳过FilterInputStream
中的字节
在本例中,我们将使用 skip()方法从 Java 中的FilterInputStream
中丢弃指定数量的字节。请看下面的例子。
import java.io.*;
public class studytonight {
public static void main(String[] args) throws Exception {
FileInputStream st = null;
FilterInputStream std = null;
int count = 0;
try {
st = new FileInputStream("E:\studytonight\output.txt");
std = new BufferedInputStream(st);
while ((count = st.read()) != -1) {
char ch = (char) count;
System.out.println("ch: " + ch);
long skip = st.skip(2);
System.out.println("st.skip(2): " + skip);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
if (std != null) {
st.close();
if (st != null) {
st.close();
}
}
}
}
}
ch:S ST . skip(2):2 ch:a ST . skip(2):2 ch:r ST . skip(2):2 ch:。 st.skip(2): 2 ch:。 st.skip(2): 2
结论
在本教程中,我们学习了 Java 中 FilterInputStream 类的skip()
方法,该方法跳过当前FilterInputStream
中给定的“n”个字节,并返回跳过的确切字节数。它只能用类对象访问,如果我们试图用类名访问方法,那么我们会得到一个错误。