Home » Java programming language

Java Reader skip() Method with Example

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

Reader Class skip() method

  • skip() method is available in java.io package.
  • skip() method is used to skip the given number of characters from this stream. It will block until some input exists, or any input/output error or stream is reached its end.
  • 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 the number of characters.
    • IOException: This exception may throw when getting any input/output error while performing.
    • IllegalArgumentException: This exception may throw when the given parameter is less than 0.

Syntax:

    public long skip(long number);

Parameter(s):

  • long 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(long number) method of Reader

import java.io.*;

public class SkipOfR {
    public static void main(String[] args) throws Exception {
        Reader r_stm = null;

        try {
            // Instantiates Reader
            r_stm = new StringReader("JavaWorld!!!!");

            for (int val = 0; val < 6; ++val) {
                // By using read() method is to
                // read the integer and represent as char
                char ch = (char) r_stm.read();

                // Display ch
                System.out.println("ch: " + ch);

                // By using skip() method is to skip
                // the given byte of data
                long skip = r_stm.skip(1);
                System.out.println("r_stm.skip(1): " + skip);
            }
        } 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();
            }
        }
    }
}

Output

ch: J
r_stm.skip(1): 1
ch: v
r_stm.skip(1): 1
ch: W
r_stm.skip(1): 1
ch: r
r_stm.skip(1): 1
ch: d
r_stm.skip(1): 1
ch: !
r_stm.skip(1): 1


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.