PushbackInputStream unread() method in Java with Examples Last Updated : 05 Jun, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The unread() method of PushbackInputStream class in Java is of three types: The unread(int b) method of PushbackInputStream class in Java is used to push back a byte by copying it to the front of the pushback buffer. After revoking this method, when the next byte is read it has the value equal to the parameter passed. Syntax: public void unread(int b) throws IOException Parameters: This method accepts one parameter b that represents the integer value which is to be pushed back. Return value: This method does not return any value. Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or if there is not enough space in the pushback buffer for the byte. Below programs illustrate unread(int) method of PushbackInputStream class in IO package: Program 1: Java // Java program to illustrate // PushbackInputStream unread(int) method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Create an array byte[] byteArray = new byte[] { 'G', 'E', 'E', 'K', 'S' }; // Create inputStream InputStream inputStr = new ByteArrayInputStream(byteArray); // Create object of // PushbackInputStream PushbackInputStream pushbackInputStr = new PushbackInputStream(inputStr); for (int i = 0; i < byteArray.length; i++) { System.out.print( (char)pushbackInputStr.read()); } // Call unread() method pushbackInputStr.unread(65); System.out.print( "\n" + (char)pushbackInputStr.read()); } } Output: GEEKS A Program 2: Java // Java program to illustrate // PushbackInputStream unread() method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Create an array byte[] byteArray = new byte[] { 'G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S' }; // Create inputStream InputStream inputStr = new ByteArrayInputStream(byteArray); // Create object of // PushbackInputStream PushbackInputStream pushbackInputStr = new PushbackInputStream(inputStr); for (int i = 0; i < byteArray.length; i++) { System.out.print( (char)pushbackInputStr.read()); } // Call unread() method pushbackInputStr.unread(90); System.out.print( "\n" + (char)pushbackInputStr.read()); } } Output: GEEKSFORGEEKS Z The unread(byte[] b) method of PushbackInputStream class in Java is used to push back an array of byte by copying it to the front of the pushback buffer. After revoking this method, when the next byte is read it has the value equal to the first element of the byte array. Syntax: public void unread(byte[] b) throws IOException Parameters: This method accepts one parameter b that represents the byte array which is to be pushed back. Return value: This method does not return any value. Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or if there is not enough space in the pushback buffer for the array byte. Below programs illustrate unread(byte[]) method of PushbackInputStream class in IO package: Program 1: Java // Java program to illustrate // PushbackInputStream unread(byte[]) method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Create an array byte[] byteArray = new byte[] { 'G', 'E', 'E', 'K', 'S' }; // Create inputStream InputStream inputStr = new ByteArrayInputStream(byteArray); // Create object of // PushbackInputStream PushbackInputStream pushbackInputStr = new PushbackInputStream(inputStr, 100); for (int i = 0; i < byteArray.length; i++) { System.out.print( (char)pushbackInputStr.read()); } // Create byte array byte[] b = new byte[] { 'A', 'B', 'C' }; // Call unread() method pushbackInputStr.unread(b); System.out.println(); for (int i = 0; i < b.length; i++) { System.out.print( (char)pushbackInputStr.read()); } } } Output: GEEKS ABC Program 2: Java // Java program to illustrate // PushbackInputStream unread(byte[]) method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Create an array byte[] byteArray = new byte[] { 'G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S' }; // Create inputStream InputStream inputStr = new ByteArrayInputStream(byteArray); // Create object of // PushbackInputStream PushbackInputStream pushbackInputStr = new PushbackInputStream(inputStr, 100); for (int i = 0; i < byteArray.length; i++) { System.out.print( (char)pushbackInputStr.read()); } // Create byte array byte[] b = new byte[] { 'X', 'Y', 'Z' }; // Call unread() method pushbackInputStr.unread(b); System.out.println(); for (int i = 0; i < b.length; i++) { System.out.print( (char)pushbackInputStr.read()); } } } Output: GEEKSFORGEEKS XYZ The unread(byte[] b, int offset, int length) method of PushbackInputStream class in Java is used to push back a part of an array of byte by copying it to the front of the pushback buffer. After revoking this method, when the next byte is read it has the value equal to the first element of the portion of the given byte array. Syntax: public void unread(byte[] b, int offset, int length) throws IOException Parameters: This method accepts three parameters: b - It represents the byte array the portion of which is to be pushed. offset - It represents the starting index of the portion of byte array. length - It represents the number of bytes to be pushed. Return value: This method does not return any value. Exceptions: This method throws IOException if the input stream is closed by calling the close() method of the same class or if there is not enough space in the pushback buffer for the array byte. Below programs illustrate unread(byte[], int, int) method of PushbackInputStream class in IO package: Program 1: Java // Java program to illustrate // PushbackInputStream // unread(byte[], int, int) method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Create an array byte[] byteArray = new byte[] { 'G', 'E', 'E', 'K', 'S' }; // Create inputStream InputStream inputStr = new ByteArrayInputStream(byteArray); // Create object of // PushbackInputStream PushbackInputStream pushbackInputStr = new PushbackInputStream(inputStr, 100); for (int i = 0; i < byteArray.length; i++) { System.out.print( (char)pushbackInputStr.read()); } // Create byte array byte[] b = new byte[] { 'A', 'B', 'C', 'D', 'E' }; // Call unread() method pushbackInputStr.unread(b, 2, 3); System.out.println(); for (int i = 0; i < 3; i++) { System.out.print( (char)pushbackInputStr.read()); } } } Output: GEEKS CDE Program 2: Java // Java program to illustrate // PushbackInputStream // unread(byte[], int, int) method import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Create an array byte[] byteArray = new byte[] { 'G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S' }; // Create inputStream InputStream inputStr = new ByteArrayInputStream(byteArray); // Create object of // PushbackInputStream PushbackInputStream pushbackInputStr = new PushbackInputStream(inputStr, 100); for (int i = 0; i < byteArray.length; i++) { System.out.print( (char)pushbackInputStr.read()); } // Create byte array byte[] b = new byte[] { 'W', 'X', 'Y', 'Z' }; // Call unread() method pushbackInputStr.unread(b, 1, 3); System.out.println(); for (int i = 0; i < 3; i++) { System.out.print( (char)pushbackInputStr.read()); } } } Output: GEEKSFORGEEKS XYZ References: 1. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#unread(int) 2. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#unread(byte%5B%5D) 3. https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#unread(byte%5B%5D, int, int) Comment More infoAdvertise with us Next Article ObjectInputStream read() method in Java with examples P pp_pankaj Follow Improve Article Tags : Java Java-Functions Java-IO package Practice Tags : Java Similar Reads PushbackInputStream read() method in Java with Examples The read() method of PushbackInputStream class in Java is of two types: The read() method of PushbackInputStream class in Java is used to read the next byte of data from the input stream. This method returns the read byte from the input stream in the form of an integer. Syntax: public int read() thr 4 min read PushbackInputStream reset() method in Java with Examples The reset() method of PushbackInputStream class in Java is used to reset the steam to the position where mark() method was called. This method does nothing for PushbackInputStream. Syntax: public void reset() throws IOException Overrides: This method overrides the reset() method of FilterInputStream 2 min read PushbackInputStream skip() method in Java with Examples The skip(long n) method of PushbackInputStream class in Java is used to skip over and discards n bytes of data from this input stream. This method first skips over the bytes in the pushback buffer, and then calls the skip method of the main input stream. It returns the actual number of bytes skipped 2 min read PushbackReader unread() method in Java with Examples The unread() method of PushbackReader class in Java is of three types: 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 th 5 min read ObjectInputStream read() method in Java with examples The read() method of the ObjectInputStream class in Java reads a byte of data. This method wont run if there is no data. Syntax: public int read() Parameters: This method does not accept any parameter. Return Value: This method returns the byte read, or -1 if the end of the stream is reached. Except 1 min read PushbackInputStream mark() method in Java with Examples The mark() method of PushbackInputStream class in Java is used to mark the current position in the input stream. This method does nothing for PushbackInputStream. Syntax: public void mark(int readlimit) Overrides: This method overrides the mark() method of FilterInputStream class. Parameters: This m 2 min read Like