Signup/Sign In

Java BufferedReader skip() Method

In this tutorial, we will learn about the skip() method of BufferedReader class in Java. The number of characters passed as parameters in the method is skipped in the stream. You can find it in java.io package.

Syntax

Below is the syntax declaration of this method, 'n' represents the number of characters to be skipped and it returns the actual number of characters skipped by the method.

public long skip(long n) throws IOException  

Example 1

In this example, we are reading the data from the file using the FileReader and BufferedReader class, we also check whether the buffered reader is ready to read by calling the ready() method. While the buffered reader is ready we print each character and skip 1 character using the skip() method we will get alternate characters at the output screen.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
        FileReader fileReader  = new FileReader("E://studytonight//output.txt"); 

        BufferedReader buffReader = new BufferedReader(fileReader); 
  
        while (buffReader.ready()) 
        { 
            System.out.print((char)buffReader.read());  
            buffReader.skip(1); 
        } 
	} 
}


Hlosuyoih

output.txt

Hello Studytonight

Example 2

Here we are implementing the program same as given in the above example, but we skip two characters in each iteration while reading from the file so this time output will be different.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class StudyTonight
{
	public static void main(String[] args) throws IOException 
	{ 
        FileReader fileReader  = new FileReader("E://studytonight//output.txt"); 

        BufferedReader buffReader = new BufferedReader(fileReader); 
  
        while (buffReader.ready()) 
        { 
            System.out.print((char)buffReader.read());  
            buffReader.skip(2); 
        } 
	} 
}


Hlsdog

output.txt

Hlsdog

Conclusion

In this tutorial, we learned about the skip() method of the BufferedReader class in Java, which is used to skip n number of characters in the stream.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.