Java.io.InputStream.skip() Method

Advertisements


Description

The java.io.InputStream.reset() method skips over and discards n bytes of data from this input stream.

Declaration

Following is the declaration for java.io.InputStream.skip(long n) method:

public long skip(long n)

Parameters

  • n -- The number of bytes to be skipped.

Return Value

The actual number of bytes skipped.

Exception

  • IOException -- if an I/O error occurs, or if the stream does not support seek.

Example

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

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      
      InputStream is = null;
      int i;
      char c;
      
      try{
         // new input stream created
         is = new FileInputStream("C://test.txt");
         
         while((i=is.read())!=-1)
         {
            // converts int to char
            c=(char)i;
            
            // prints character
            System.out.println("Character Read: "+c);
            
            // skip one byte
            is.skip(1);
         }
      }catch(Exception e){
         
         // if any I/O error occurs
         e.printStackTrace();
      }finally{
         
         // releases system resources associated with this stream
         if(is!=null)
            is.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:

ABCDE

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

Character Read: A
Character Read: C
Character Read: E



Advertisements
Advertisements