Java PushbackInputStream ClassLast Updated : 28 Jan 2026 The PushbackInputStream class in Java is used to read data from an input stream with the ability to push back bytes that have already been read. In this chapter, we will understand what the PushbackInputStream class is, why it is used, its declaration, constructors, important methods, and how unread functionality works using an example. What is PushbackInputStream Class in Java?The PushbackInputStream class extends FilterInputStream and provides additional functionality to another input stream. It allows a program to unread one or more bytes after they have been read. This feature is useful when a program needs to inspect input data and then decide whether to process it or push it back for re-reading later. It is commonly used in parsing scenarios. PushbackInputStream Class DeclarationThe following declaration shows how the PushbackInputStream class is defined: Constructors of PushbackInputStream ClassThe PushbackInputStream class provides constructors to create a pushback buffer. 1. PushbackInputStream(InputStream in)This constructor creates a PushbackInputStream with a default pushback buffer size of one byte. Syntax: Here is the syntax: 2. PushbackInputStream(InputStream in, int size))This constructor creates a PushbackInputStream with a specified pushback buffer size. Syntax: Here is the syntax: Example of PushbackInputStream Class ConstructorsThe following example shows how the constructor of the PushbackInputStream class is used. Methods of PushbackInputStream ClassThe PushbackInputStream class provides the following methods to read and unread bytes.
Examples of Java PushbackInputStream ClassPractice the following examples to understand the methods and usages of PushbackInputStream class. Example 1: Using PushbackInputStream to Replace CharactersThe following example demonstrates how to unread bytes and replace specific characters using the PushbackInputStream class. Output: 1**2#34**#12 Example 2: Unreading a Byte After Look-AheadThe following example demonstrates how a byte can be read, checked, and then pushed back into the stream if required. Output: Read character: A Re-read character: A Next TopicJava PushbackReader Class |
We request you to subscribe our newsletter for upcoming updates.