java.io.BufferedReader skip()

Description :

This java tutorial shows how to use the skip() method of BufferedReader class of java.io package. This method returns how many characters has been skipped. This method override the skip() method of Reader Class.

Method Syntax :

public long skip(long n) throws IOException

Parameter Input :

 

DataType Parameter Description
long n It corresponds to the number of characters to skip

 

Method Returns :

This method simply returns how many characters were skipped.

Compatibility Version :

Requires Java 1.1 and up

Exception :

IllegalArgumentException

– the IllegalArgumentException will be thrown by skip() method if the argument n is negative

IOException

– IOException will be thrown if I/O error occurs

Discussion :

The BufferedReader skip() method takes an argument of long which signifies how many characters to be skip on the inputstream. This is used when we are caught in a scenario on which on reading characters on the input stream we want to omit character sequences.

Java Code Example :

This java example source code demonstrates the use of skip() method of BufferedReader class. Basically we read the user input from the console and then we skip 8 characters using the skip method. The readLine() method were used to print what is remaining on the input stream after 8 characters were skipped.

package com.javatutorialhq.java.examples;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*
 * Java example source code that uses the skip() method of
 * BufferedReader class
 * This example java program reads a user input and 
 * then we skip a certain number of characters
 */

public class BufferedReaderSkip {

	public static void main(String[] args) {
		System.out.print("Enter a sample string: ");
		// declare the BufferedReader Class
		// used the InputStreamReader to read the console input
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));

		// catch the possible IOException by the read() method
		try {
			// skip 8 characters on the input stream
			reader.skip(8);
			// print what is remaining on the input stream
			System.out.println("Output:"+reader.readLine());

			// close the BufferedReader object
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

Sample Output :

Running the skip() method example source code of BufferedReader class will give you the following output

Enter a sample string: BufferedReader skip method example
Output:Reader skip method example

Similar Method :

  • N/A

Suggested Reading List :

References :