Home » Java programming language

Java StringReader skip() Method with Example

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

StringReader Class skip() method

  • skip() method is available in java.io package.
  • skip() method is used to skip the given number of characters in the stream.
  • 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.

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 StringReader

import java.io.*;

public class SkipOfSR {
    public static void main(String[] args) throws Exception {
        StringReader str_r = null;

        try {
            // Instantiates StringReader
            str_r = new StringReader("Java World");

            // Loop to read until available
            // bytes left
            for (int val = 0; val <= 4; ++val) {
                // Read corresponding char value
                char ch = (char) str_r.read();

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

                // By using skip() method is
                // to skip 1 bytes of data
                // from str_r	    

                long skip = str_r.skip(1);
                System.out.println("str_r.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 (str_r != null) {
                str_r.close();
            }
        }
    }
}

Output

ch: J 
str_r.skip(1): 1
ch: v 
str_r.skip(1): 1
ch:   
str_r.skip(1): 1
ch: o 
str_r.skip(1): 1
ch: l 
str_r.skip(1): 1


Comments and Discussions!

Load comments ↻





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