Home » Java programming language

Java PushbackReader skip() Method with Example

PushbackReader Class skip() method: Here, we are going to learn about the skip() method of PushbackReader Class with its syntax and example.
Submitted by Preeti Jain, on April 20, 2020

PushbackReader Class skip() method

  • skip() method is available in java.io package.
  • skip() method is used to skip the given number of characters from this PushbackReader stream and it will block until some character input exists or any input/output error occurs or the end-of-stream is reached.
  • skip() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • skip() method may throw an exception at the time of skipping bytes of data.
    • IOException: This exception may throw when getting any input/output error.
    • IllegalArgumentException: This exception may throw when the given parameter is less than 0.

Syntax:

    public long skip(int number);

Parameter(s):

  • int number – represents the number of characters to skip.

Return value:

The return type of the method is long, it returns the exact number of characters skipped.

Example:

// Java program to demonstrate the example 
// of long skip(int number) method of
// PushbackReader

import java.io.*;

public class SkipOfPBR {
    public static void main(String[] args) throws Exception {
        Reader r_stm = null;
        PushbackReader pb_r = null;
        int val = 1;
        
        try {
            // Instantiates Reader and PushbackReader
            r_stm = new StringReader("Java World!!!!");
            pb_r = new PushbackReader(r_stm);

            for (int i = 0; i < 4; ++i) {
                // By using read() method is to 
                // read int and convert it into 
                // char
                char ch = (char) pb_r.read();
                System.out.println("ch: " + ch);

                // By using skip() method is to 
                // skip the given byte of char from
                // pb_r
                long skip = pb_r.skip(val);
                System.out.println("pb_r.skip(val): " + skip);
                val = val + 1;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        } finally {
            // with the help of this block is to
            // free all necessary resources linked
            // with the stream
            if (r_stm != null) {
                r_stm.close();
                if (pb_r != null) {
                    pb_r.close();

                }
            }
        }
    }
}

Output

ch: J
pb_r.skip(val): 1
ch: v
pb_r.skip(val): 2
ch: W
pb_r.skip(val): 3
ch: d
pb_r.skip(val): 4





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.