LineNumberReader reset() in Java

reset(): This method is available in the java.io.LineNumberReader class of Java.

Syntax:

void java.io.LineNumberReader.reset() throws IOException

This method resets the stream to the most recent mark.

Parameters: NA

Returns: NA

Throws:

IOException - If the stream has not been marked, or if the mark has been invalidated.

Approach 1: When no exception

Java

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;

public class LineNumberReaderreset {
    public static void main(String[] args) {

        Reader in = new StringReader("Hello");
        LineNumberReader lineNumberReader =
new LineNumberReader(in);

        try {
            lineNumberReader.mark(0);
            lineNumberReader.reset();
            System.out.println("Successfully reset");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

    }
}

Output:

Successfully reset


Approach 2: IOException

Java

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;

public class LineNumberReaderreset {
    public static void main(String[] args) {

        Reader in = new StringReader("Hello");
        LineNumberReader lineNumberReader =
new LineNumberReader(in);

        try {
            lineNumberReader.reset();
            System.out.println("Successfully reset");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

    }
}

Output:

IOException occurs


Some other methods of LineNumberReader

getLineNumber()This method gets the current line number.

LineNumberReader(Reader)This method creates a new line-numbering reader, using the default input buffer size.

LineNumberReader(Reader, int)This method creates a new line-numbering reader, reading characters into a buffer of the given size.

mark(int)This method marks the present position in the stream.

read()This method read a single character.

read(char[], int, int)This method read characters into a portion of an array.

readLine()This method read a line of text.

setLineNumber(int)This method sets the current line number.

skip(long)This method skips characters.

No comments:

Post a Comment