Java.io.FileInputStream.skip() 方法
描述
java.io.FileInputStream.skip(long n) 跳过并丢弃输入流中的 n 字节数据。
声明
以下是 java.io.FileInputStream.skip(long n) 方法的声明 −
public long skip(long n)
参数
n − 要跳过的字节数。
返回值
该方法返回实际跳过的字节数。
异常
IOException − 如果发生 I/O 错误、n 为负数或流不支持查找。
示例
下面的例子展示了 java.io.FileInputStream.skip(long n) 方法的使用。
package com.tutorialspoint;
import java.io.IOException;
import java.io.FileInputStream;
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
int i = 0;
char c;
try {
// create new file input stream
fis = new FileInputStream("C://test.txt");
// skip bytes from file input stream
fis.skip(4);
// read bytes from this stream
i = fis.read();
// converts integer to character
c = (char)i;
// prints
System.out.print("Character read: "+c);
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
} finally {
// releases all system resources from the streams
if(fis!=null)
fis.close();
}
}
}
假设我们有一个文本文件c:/test.txt,其内容如下。 该文件将用作我们示例程序的输入 −
ABCDEF
让我们编译并运行上面的程序,这将产生下面的结果 −
Character read: E

