Java.io.BufferedReader.skip() Method



Description

The java.io.BufferedReader.skip(long n) skips n numbers of characters.

Declaration

Following is the declaration for java.io.BufferedReader.skip() method.

public long skip(long n)

Parameters

n − n number of characters to be skipped.

Return Value

This method returns actual number of characters skipped.

Exception

  • IOException − If some I/O error occurs.

  • IllegalArgumentException − If n is negative.

Example

The following example shows the usage of java.io.BufferedReader.skip() method.

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.StringReader;

public class BufferedReaderDemo {
   public static void main(String[] args) throws Exception {
      String s ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      StringReader sr = null; 
      BufferedReader br = null;

      try {
         // create and assign a new string reader
         sr = new StringReader(s);
         
         // create  new buffered reader
         br = new BufferedReader(sr);

         // reads and prints BufferedReader
         int value = 0;
         
         while((value = br.read()) != -1) {
         
            // skips a character
            br.skip(1);
            System.out.print((char)value);
         }
         
      } catch (Exception e) {
         // exception occurred.
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(sr!=null)
            sr.close();
         if(br!=null)
            br.close();
      }
   }
}

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

ACEGIKMOQSUWY
java_io_bufferedreader.htm
Advertisements