Java.io.FileInputStream.skip() Method


Advertisements


Description

The java.io.FileInputStream.skip(long n) skips over and discards n bytes of data from the input stream.

Declaration

Following is the declaration for java.io.FileInputStream.skip(long n) method −

public long skip(long n)

Parameters

n − The number of bytes to be skipped.

Return Value

The method returns the actual number of bytes skipped.

Exception

IOException − If an I/O error occurs, if n is negative or if the stream does not support seek.

Example

The following example shows the usage of java.io.FileInputStream.skip(long n) method.

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();
      }
   }
}

Assuming we have a text file c:/test.txt, which has the following content. This file will be used as an input for our example program −

ABCDEF

Let us compile and run the above program, this will produce the following result −

Character read: E

java_io_fileinputstream.htm

Advertisements