Java InputStream Class
InputStream is the abstract base class for reading byte data in the Java I/O system, located in the java.io package.
InputStream is the parent class of all byte input streams, providing the basic framework for reading byte data.
InputStream is an abstract class, which means you cannot instantiate it directly, but you can create concrete input stream objects through its subclasses (such as FileInputStream, ByteArrayInputStream, etc.).
Core Method Analysis
InputStream is part of the Java IO streams, used to read data byte by byte (8 bits).
InputStream is suitable for processing non-text types of data, such as images, audio, video, compressed files, etc.
You can think of it as a "water pipe", with data flowing from the source into your program like water.

Core Methods
| Method Names | Description |
|---|---|
int read() |
Reads one byte; the return value is an int from 0 to 255, or if the end of the stream is reached, returns -1 |
int read(byte[] b) |
Reads at most from the stream b.length bytes of data and stores them into the byte array b |
int read(byte[] b, int off, int len) |
From the off starts reading at the offset position, at most len bytes into the array b |
void close() |
Closes the stream and releases the resources associated with it |
Reading a Single Byte
public
abstract
int
read
(
)
throws
IOException
This is the most basic method in InputStream, used to read the next byte of data from the input stream. The return value is an int between 0 and 255, or -1 if the end of the stream is reached.
Example Code
Example
InputStream
input
=
new
FileInputStream
(
"example.txt"
)
;
int
data
=
input.
read
(
)
;
while
(
data
!=
1
)
{
System
.
out
.
(
(
char
)
data
)
;
data
=
input.
read
(
)
;
}
input.
close
(
)
;
Reading a Byte Array
Example
public
int
read
(
byte
[
]
b
)
throws
IOException
This method attempts to read at most b.length bytes of data into the byte array, returning the actual number of bytes read, or -1 if the end of the stream is reached.
Example Code
Example
InputStream
input
=
new
FileInputStream
(
"example.txt"
)
;
byte
[
]
buffer
=
new
byte
[
1024
]
;
int
bytesRead
;
while
(
(
bytesRead
=
input.
read
(
buffer
)
)
!=
1
)
{
System
.
out
.
println
(
"Read "
bytesRead
" bytes"
)
;
}
input.
close
(
)
;
Reading a Specified Length of Bytes
Example
public
int
read
(
byte
[
]
b,
int
off,
int
len
)
throws
IOException
This method allows you to specify the position (off) from which to start storing the read data, as well as the maximum number of bytes (len) to read.
Example Code
Example
InputStream
input
=
new
FileInputStream
(
"example.txt"
)
;
byte
[
]
buffer
=
new
byte
[
1024
]
;
int
bytesRead
=
input.
read
(
buffer,
10
,
500
)
;
// Store starting from buffer[10], reading at most 500 bytes
input.
close
(
)
;
Skipping Bytes
Example
public
long
skip
(
long
n
)
throws
IOException
Skips and discards n bytes of data from the input stream, returning the actual number of bytes skipped.
Checking Available Bytes
Example
public
int
available
(
)
throws
IOException
Returns an estimate of the number of bytes that can be read (or skipped) from this input stream without blocking.
2.6 Closing the Stream
Example
public
void
close
(
)
throws
IOException
Closes the input stream and releases all system resources associated with it. This is a very important method and should be called after completing stream operations.
Introduction to Common Subclasses
- FileInputStream: Used to read data from a file.
- ByteArrayInputStream: Allows a byte array in memory to be used as an input stream.
- FilterInputStream: A decorator class that provides additional functionality for other input streams.
- ObjectInputStream: Used to deserialize objects previously serialized with ObjectOutputStream.
- PipedInputStream: Used together with PipedOutputStream to implement inter-thread pipe communication.
Best Practices
Using try-with-resources
Java 7 introduced the try-with-resources statement, which can automatically close resources that implement the AutoCloseable interface.
Example
try
(
InputStream
input
=
new
FileInputStream
(
"example.txt"
)
)
{
int
data
;
while
(
(
data
=
input.
read
(
)
)
!=
1
)
{
System
.
out
.
(
(
char
)
data
)
;
}
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
Buffered Reading Improves Performance
For large files or frequent read operations, it is recommended to wrap with BufferedInputStream to improve performance.
Example
try
(
InputStream
input
=
new
BufferedInputStream
(
new
FileInputStream
(
"largefile.dat"
)
)
)
{
// Read operation
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
Handling Exceptions Properly
IO operations may throw IOException, and these exceptions should be handled properly.
5. Frequently Asked Questions
Why does the read() method return int instead of byte?
Because a special value (-1) is needed to indicate the end of the stream, and the byte type cannot represent -1.
How to convert an InputStream to a String?
Example
public
static
String
convertToString
(
InputStream
input
)
throws
IOException
{
try
(
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
input
)
)
)
{
StringBuilder stringBuilder
=
new
StringBuilder
(
)
;
String
line
;
while
(
(
line
=
reader.
readLine
(
)
)
!=
null
)
{
stringBuilder.
append
(
line
)
;
}
return
stringBuilder.
toString
(
)
;
}
}
How to copy an InputStream?
An InputStream can usually be read only once. If you need to read it multiple times, you can copy its content into a byte array or a ByteArrayInputStream.
6. Summary
The InputStream class is one of the core components of the Java I/O system, providing the basic functionality for reading byte data. Understanding InputStream and its subclasses is essential for handling various input sources (such as files, network connections, memory buffers, etc.). In practical applications, attention should be paid to resource management, exception handling, and performance optimization.