Skip to content
Related Articles

Related Articles

PushbackReader unread() method in Java with Examples

Improve Article
Save Article
Like Article
  • Last Updated : 05 Jun, 2020

The unread() method of PushbackReader class in Java is of three types:

  1. The unread(int c) method of PushbackReader class in Java is used to push back a character by copying it to the front of the pushback buffer. After revoking this method, when the next character is read it has the value equal to the parameter passed.

    Syntax:

    public void unread(int c)
                throws IOException
    

    Parameters: This method accepts one parameter c that represents the integer value of a character which is to be pushed back.

    Return value: This method does not return any value.

    Exceptions: This method throws IOException if the pushback buffer is full or if an I/O error occurs.

    Below programs illustrate unread(int) method of PushbackReader class in IO package:

    Program 1:




    // Java program to illustrate
    // PushbackReader unread(int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create string
            String str = "GEEKS";
      
            // Create stringReader
            StringReader strReader
                = new StringReader(str);
      
            // Create object of
            // PushbackReader
            PushbackReader pushbackReader
                = new PushbackReader(strReader, 100);
      
            for (int i = 0; i < str.length(); i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
      
            // Call unread() method
            pushbackReader.unread(65);
      
            System.out.print(
                "\n"
                + (char)pushbackReader.read());
        }
    }
    Output:
    GEEKS
    A
    

    Program 2:




    // Java program to illustrate
    // PushbackReader unread(int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create string
            String str = "GEEKSFORGEEKS";
      
            // Create stringReader
            StringReader strReader
                = new StringReader(str);
      
            // Create object of
            // PushbackReader
            PushbackReader pushbackReader
                = new PushbackReader(strReader, 100);
      
            for (int i = 0; i < str.length(); i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
      
            // Call unread() method
            pushbackReader.unread('Z');
      
            System.out.print(
                "\n"
                + (char)pushbackReader.read());
        }
    }
    Output:
    GEEKSFORGEEKS
    Z
    
  2. The unread(char[] cbuf) method of PushbackReader class in Java is used to push back an array of characters by copying it to the front of the pushback buffer. After revoking this method, when the next character is read it has the value equal to the first element of the character array.

    Syntax:

    public void unread(char[] cbuf)
                throws IOException
    

    Parameters: This method accepts one parameter cbuf that represents the character array which is to be pushed back.

    Return value: This method does not return any value.

    Exceptions: This method throws IOException if the pushback buffer is full or if an I/O error occurs.

    Below programs illustrate unread(char[]) method of PushbackReader class in IO package:

    Program 1:




    // Java program to illustrate
    // PushbackReader unread(char[]) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create string
            String str = "GEEKS";
      
            // Create stringReader
            StringReader strReader
                = new StringReader(str);
      
            // Create object of
            // PushbackReader
            PushbackReader pushbackReader
                = new PushbackReader(strReader, 100);
      
            for (int i = 0; i < str.length(); i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
      
            // Create character array
            char[] cbuf = new char[] { 'A', 'B', 'C' };
      
            // Call unread() method
            pushbackReader.unread(cbuf);
      
            System.out.println();
      
            for (int i = 0; i < cbuf.length; i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
        }
    }
    Output:
    GEEKS
    ABC
    

    Program 2:




    // Java program to illustrate
    // PushbackReader unread(char[]) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create string
            String str = "GEEKSFORGEEKS";
      
            // Create stringReader
            StringReader strReader
                = new StringReader(str);
      
            // Create object of
            // PushbackReader
            PushbackReader pushbackReader
                = new PushbackReader(strReader, 100);
      
            for (int i = 0; i < str.length(); i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
      
            // Create character array
            char[] cbuf = new char[] { 'X', 'Y', 'Z' };
      
            // Call unread() method
            pushbackReader.unread(cbuf);
      
            System.out.println();
      
            for (int i = 0; i < cbuf.length; i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
        }
    }
    Output:
    GEEKSFORGEEKS
    XYZ
    
  3. The unread(char[] cbuf, int offset, int length) method of PushbackReader class in Java is used to push back a part of an array of characters by copying it to the front of the pushback buffer. After revoking this method, when the next character is read it has the value equal to the first element of the portion of the given character array.

    Syntax:

    public void unread(char[] cbuf,
                       int offset,
                       int length)
                throws IOException
    

    Parameters: This method accepts three parameters:

    • cbuf – It represents the character array the portion of which is to be pushed.
    • offset – It represents the starting index of the portion of character array.
    • length – It represents the number of characters to be pushed.

    Return value: This method does not return any value.

    Exceptions: This method throws IOException if there is not sufficient space in the pushback buffer or if an I/O error occurs.

    Below programs illustrate unread(char[], int, int) method of PushbackReader class in IO package:

    Program 1:




    // Java program to illustrate
    // PushbackReader
    // unread(char[], int, int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create string
            String str = "GEEKS";
      
            // Create stringReader
            StringReader strReader
                = new StringReader(str);
      
            // Create object of
            // PushbackReader
            PushbackReader pushbackReader
                = new PushbackReader(strReader, 100);
      
            for (int i = 0; i < str.length(); i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
      
            // Create character array
            char[] cbuf
                = new char[] { 'A', 'B', 'C',
                               'D', 'E' };
      
            // Call unread() method
            pushbackReader.unread(cbuf, 2, 3);
      
            System.out.println();
      
            for (int i = 0; i < 3; i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
        }
    }
    Output:
    GEEKS
    CDE
    

    Program 2:




    // Java program to illustrate
    // PushbackReader
    // unread(char[], int, int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
            throws IOException
        {
      
            // Create string
            String str = "GEEKSFORGEEKS";
      
            // Create stringReader
            StringReader strReader
                = new StringReader(str);
      
            // Create object of
            // PushbackReader
            PushbackReader pushbackReader
                = new PushbackReader(strReader, 100);
      
            for (int i = 0; i < str.length(); i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
      
            // Create character array
            char[] cbuf
                = new char[] { 'W', 'X',
                               'Y', 'Z' };
      
            // Call unread() method
            pushbackReader.unread(cbuf, 1, 3);
      
            System.out.println();
      
            for (int i = 0; i < 3; i++) {
                System.out.print(
                    (char)pushbackReader.read());
            }
        }
    }
    Output:
    GEEKSFORGEEKS
    XYZ
    

References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackReader.html#unread(int)
2. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackReader.html#unread(char%5B%5D)
3. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackReader.html#unread(char%5B%5D, int, int)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!