java.io.RandomAccessFile.skipBytes(int n) 方法尝试跳过n个字节的输入丢弃跳过的字节。此方法可能跳过某些较小的字节数,可能为零。这可能是由于任意数量的条件;到达文件的末尾n个字节被跳过之前只有一种可能。这种方法不会抛出抛出:EOFException。返回的实际跳过的字节数。如果n为负,则不跳过任何字节。
声明
以下是java.io.RandomAccessFile.skipBytes()方法的声明
public int skipBytes(int n)
参数
-
n -- 要跳过的字节数。
返回值
此方法返回的实际跳过的字节数。
异常
-
IOException -- 如果发生I/ O错误。
例子
下面的示例演示java.io.RandomAccessFile.skipBytes()方法的用法。
package com.yiibai; import java.io.*; public class RandomAccessFileDemo { public static void main(String[] args) { try { // create a new RandomAccessFile with filename test RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF("Hello World"); // set the file pointer at 0 position raf.seek(0); // print the string System.out.println("" + raf.readUTF()); // set the file pointer at 0 position raf.seek(0); // attempt to skip 10 bytes and print the number of bytes skipped System.out.println("" + raf.skipBytes(10)); // print what is left after skipping System.out.println("" + raf.readLine()); // set the file pointer to position 8 raf.seek(8); // attempt to skip 10 more bytes and print the number of bytes skipped System.out.println("" + raf.skipBytes(10)); } catch (IOException ex) { ex.printStackTrace(); } } }
假设我们有一个文本文件c:/ test.txt,它具有以下内容。该文件将被用作输入到我们的示例程序:
ABCDE
让我们编译和运行上面的程序,这将产生以下结果:
Hello World 10 rld 5