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
java_io_fileinputstream.htm
广告

© . All rights reserved.