PushbackReader unread(char[]) in Java

unread(char[]): This method is available in the java.io.PushbackReader class of Java.

Syntax:

void java.io.PushbackReader.unread(char[] cbuf) throws IOException

This method takes one argument. This method pushes back an array of characters by copying it to the front of the pushback buffer.

After this method returns, the next character to be read will have the value cbuf[0], the character after that will have the value cbuf[1], and so forth.

Parameters: One parameter is required for this method.

cbuf: Character array to push back.

Returns: NA

Throws:

1. IOException - If there is insufficient room in the pushback buffer, or if some other I/O error occurs

Approach 1: When no exception

Java

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

public class PushbackReaderunread {
    public static void main(String[] args) throws IOException {

        Reader in = new StringReader("HELLO");
        int size = 10;
        PushbackReader pushbackReader =
new PushbackReader(in, size);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };
        pushbackReader.unread(cbuf);

        System.out.println("Successfully unread");
        pushbackReader.close();
    }
}

Output:

Successfully unread


Approach 2: IOException

Java

package com.PushbackReader;

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

public class PushbackReaderunread {
    public static void main(String[] args) throws IOException {

        Reader in = new StringReader("HELLO");
        PushbackReader pushbackReader = new PushbackReader(in);

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        pushbackReader.unread(cbuf);

        System.out.println("Successfully unread");
        pushbackReader.close();
    }
}

Output:

Exception in thread "main" java.io.IOException: Pushback buffer overflow at java.base/java.io.PushbackReader.unread(PushbackReader.java:179) at java.base/java.io.PushbackReader.unread(PushbackReader.java:197) at com.PushbackReader.PushbackReaderunread.main(PushbackReaderunread.java:15)


Some other methods of PushbackReader

close()This method closes the stream and releases any system resources associated with it.

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

markSupported()This method tells whether this stream supports the mark() operation, which it does not.

PushbackReader(Reader)This method creates a new pushback reader with a one-character pushback buffer.

PushbackReader(Reader, int)This method creates a new pushback reader with a pushback buffer of the given size.

read()This method reads a single character.

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

ready()This method tells whether this stream is ready to be read.

reset()This method resets the stream.

skip(long)This method skips characters

unread(char[])This method pushes back an array of characters by copying it to the front of the pushback buffer.

unread(int)This method pushes back a single character by copying it to the front of the pushback buffer.

unread(char[], int, int) This method pushes back a portion of an array of characters by copying it to the front of the pushback buffer.

No comments:

Post a Comment