Java - PushbackReader unread(char[] cbuf,int off,int len) method



Description

The Java PushbackReader unread(char[] cbuf,int off,int len) method pushes back a portion of 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[off], the character after that will have the value cbuf[off+1], and so forth.

unread(char[] cbuf,int off,int len) method −

  • Purpose− Pushes back a subrange of a character array into the stream.

  • off is the starting offset in the array, and len is the number of characters to unread.

  • You must ensure the unread buffer is large enough, or an IOException (buffer overflow) will be thrown.

Declaration

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

public void unread(char[] cbuf,int off,int len)

Parameters

  • cbuf − Character array to push back.

  • off − Offset of first character to push back.

  • len − Number of characters 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,int off,int len) method

The following example shows the usage of PushbackReader unread(char[] cbuf,int off,int len) 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, 1, 4);

         // read 4 chars, which is what we unread from cbuf
         for (int i = 0; i < 4; 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
orld

Example - Unread a portion of a character array

The following example shows the usage of PushbackReader unread(char[] cbuf,int off,int len) 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("ABCDEF"), 10)) {
         char[] message = {'H', 'e', 'l', 'l', 'o', '!', '!'};

         // Push back "lo!" (starting at index 3, length 3)
         reader.unread(message, 3, 3);

         // Read back the pushed characters
         char[] buffer = new char[3];
         reader.read(buffer);
         System.out.println("Read after unread: " + new String(buffer));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read after unread: lo!

Explanation

  • The original stream is "ABCDEF" but we push back "lo!".

  • These characters are read before the original stream resumes.

Example - Read some characters, then push back part of what was read

The following example shows the usage of PushbackReader unread(char[] cbuf,int off,int len) 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("Example"), 10)) {
         char[] buffer = new char[4];
         reader.read(buffer); // Reads "Exam"
         System.out.println("Initially read: " + new String(buffer));

         // Push back "am" (from index 2, length 2)
         reader.unread(buffer, 2, 2);
         System.out.println("Unread characters: " + new String(buffer, 2, 2));

         // Read again
         char[] rebuffer = new char[2];
         reader.read(rebuffer);
         System.out.println("Read again after unread: " + new String(rebuffer));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Initially read: Exam  
Unread characters: am  
Read again after unread: am

Explanation

  • After reading "Exam", we push back the last two characters "am".

  • These are then re-read, showing how the method lets you "replay" part of the stream.

java_io_pushbackreader.htm
Advertisements