Java - PushbackReader unread(char[] cbuf) method



Description

The Java PushbackReader unread(char[] cbuf) 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.

unread(char[] cbuf) method −

  • Purpose − Pushes back the entire character array cbuf so they can be read again later.

  • Must ensure the pushback buffer size is at least as large as the array.

  • If buffer overflows, it throws IOException.

Declaration

Following is the declaration for java.io.PushbackReader.unread(char[] cbuf) method.

 public void unread(char[] cbuf) 

Parameters

cbuf − Character array to push back.

Return Value

This method does not return a value.

Exception

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

Example - Usage of PushbackReader unread(char[] cbuf) method

The following example shows the usage of PushbackReader unread(char[] cbuf) method.

PushbackReaderDemo.java

 package com.tutorialspoint; import java.io.IOException; import java.io.PushbackReader; import java.io.StringReader; public class PushbackReaderDemo { public static void main(String[] args) { String s = "Hello World"; // create a new StringReader StringReader sr = new StringReader(s); // create a new PushBack reader based on our string reader PushbackReader pr = new PushbackReader(sr, 20); try { // read the first five chars for (int i = 0; i < 5; i++) { char c = (char) pr.read(); System.out.print("" + c); } // change line System.out.println(); // create a new array to unread char cbuf[] = {'w', 'o', 'r', 'l', 'd'}; // unread into cbuf pr.unread(cbuf); // read five chars, which is what we unread from cbuf for (int i = 0; i < 5; i++) { char c = (char) pr.read(); System.out.print("" + c); } // close the stream pr.close(); } catch (IOException ex) { ex.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result −

 Hello world 

Example - Push back characters and read them again

The following example shows the usage of PushbackReader unread(char[] cbuf) method.

PushbackReaderDemo.java

 package com.tutorialspoint; import java.io.IOException; import java.io.PushbackReader; import java.io.StringReader; public class PushbackReaderDemo { public static void main(String[] args) { try (PushbackReader reader = new PushbackReader(new StringReader("Java"), 10)) { char[] charsToUnread = {'H', 'e', 'l', 'l', 'o'}; // Push back the characters "Hello" reader.unread(charsToUnread); // Now read them back char[] buffer = new char[5]; int read = reader.read(buffer); System.out.println("Read after unread: " + new String(buffer, 0, read)); } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Read after unread: Hello 

Explanation

  • "Java" is ignored because "Hello" is pushed back first.

  • The pushed-back chars are read before anything from the original stream.

Example - Combine reading and unreading

The following example shows the usage of PushbackReader unread(char[] cbuf) method.

PushbackReaderDemo.java

 package com.tutorialspoint; import java.io.IOException; import java.io.PushbackReader; import java.io.StringReader; public class PushbackReaderDemo { public static void main(String[] args) { try (PushbackReader reader = new PushbackReader(new StringReader("PushBack"), 10)) { // Read first 4 characters: "Push" char[] buffer = new char[4]; reader.read(buffer); System.out.println("Initially read: " + new String(buffer)); // Push them back reader.unread(buffer); System.out.println("Unread the same characters"); // Read again char[] buffer2 = new char[4]; reader.read(buffer2); System.out.println("Read again after unread: " + new String(buffer2)); } catch (IOException e) { e.printStackTrace(); } } } 

Output

Let us compile and run the above program, this will produce the following result−

 Initially read: Push Unread the same characters Read again after unread: Push 

Explanation

  • The first 4 characters are read, then pushed back using unread(char[]).

  • When read again, the same characters are retrieved.

java_io_pushbackreader.htm
Advertisements