Signup/Sign In

Java CharArrayReader skip() Method

In this tutorial, we will learn about the skip() method of the CharArrayReader class of Java. The task of this method is to skip the provided number of characters, on the stream. The stream gets blocked or an exception is thrown by the method, in case it reaches the end of the stream by skipping.

Syntax

Here is the syntax of this method, in which the number of characters to be passed is accepted as a parameter, and the number of characters actually skipped is returned by the method.

public long skip(long n) throws IOException  

Example: Skip Char using skip() Method in Java

In this example, we are using the skip() method to skip the characters on the stream, while calling this method we passed 2 as the number of characters to be skipped and that's why the output is showing data from the stream after skipping the 2 characters.

import java.io.CharArrayReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try
		{
			char[] ch = { 'A', 'B', 'C', 'D', 'E' };  
			CharArrayReader charArrayReader = new CharArrayReader(ch);  
			int value = 0;  
			while ((value = charArrayReader.read()) != -1) 
			{  
				char c = (char) value;  
				System.out.print(c + " ");  
				charArrayReader.skip(2);  
			}  
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	} 
}


A D

Example 2: Skip Char using CharArrayReader

Here, we are using the skip() method to skip 1 character after reading the data from the stream. When we read the data from this stream we will get the alternate data output from this stream.

import java.io.CharArrayReader;
class StudyTonight
{
	public static void main(String[] args)  
	{ 
		try
		{
			char[] ch = { 'A', 'B', 'C', 'D', 'E' };  
			CharArrayReader charArrayReader = new CharArrayReader(ch);  
			int value = 0;  
			while ((value = charArrayReader.read()) != -1) 
			{  
				char c = (char) value;  
				System.out.print(c + " ");  
				charArrayReader.skip(1);  
			}  
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	} 
}


A C E

Conclusion

In this tutorial, we learned about the skip() method of the CharArrayReader class of Java, which is used to skip the specified number of characters on the stream, the number of which is passed as a parameter in the method.



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.