Java InputStream and OutputStream Classes
When working with files, network connections, or any other data sources in Java, you need a way to read and write data efficiently. This is where Java's InputStream and OutputStream classes come into play. They form the foundation of Java's I/O (Input/Output) system for handling byte-based data.
In this blog, we will explore these two fundamental classes, understand their purpose, and list the key classes that extend them. This will set the stage for our detailed exploration of specific stream classes in upcoming blogs.
1. Understanding InputStream and OutputStream#
Java follows a stream-based approach to handling input and output. A stream is a sequence of data that flows from a source to a destination.
- InputStream is used for reading data (input) from a source.
- OutputStream is used for writing data (output) to a destination.
Both of these are abstract classes present in the java.io
package, meaning they provide a blueprint for specific stream classes to extend and implement functionality.
2. Methods in InputStream Class#
The InputStream
class provides methods to read bytes from a source. Here are the key methods it defines:
● int read()
#
- Reads a single byte of data and returns it as an
int
. Returns1
if the end of the stream is reached.
● int read(byte[] b)
#
- Reads bytes into the provided byte array
b
and returns the number of bytes read.
● int read(byte[] b, int off, int len)
#
- Reads up to
len
bytes into the array starting atoff
index.
● void close()
#
- Closes the stream and releases resources.
● int available()
#
- Returns the number of available bytes that can be read without blocking.
● long skip(long n)
#
- Skips
n
bytes and returns the actual number of bytes skipped.
3. Methods in OutputStream Class#
The OutputStream
class provides methods to write bytes to a destination. Here are the key methods it defines:
● void write(int b)
#
- Writes a single byte to the output stream.
● void write(byte[] b)
#
- Writes an entire byte array to the output stream.
● void write(byte[] b, int off, int len)
#
- Writes
len
bytes from the array starting at indexoff
.
● void flush()
#
- Forces any buffered output to be written immediately.
● void close()
#
- Closes the stream and releases any resources.
4. Classes Extending InputStream#
Several classes extend InputStream
to provide specific functionalities. These will be covered in detail in upcoming blogs:
- FileInputStream - Reads data from a file.
- ByteArrayInputStream - Reads data from a byte array.
- ObjectInputStream - Reads objects from a stream (used in serialization).
- BufferedInputStream - Wraps another
InputStream
to improve performance.
5. Classes Extending OutputStream#
Similar to InputStream
, the OutputStream
class has multiple implementations for different use cases:
- FileOutputStream - Writes data to a file.
- ByteArrayOutputStream - Writes data to a byte array.
- ObjectOutputStream - Writes objects to a stream (used in serialization).
- BufferedOutputStream - Wraps another
OutputStream
for better performance. - PrintStream - Used to print formatted representations of objects.
Conclusion#
In this blog, we explored the foundational InputStream
and OutputStream
classes in Java. These classes define core methods for reading and writing byte-based data, and various specialized classes extend them to handle different sources and destinations.
In the next blogs, we will dive deeper into each of these specific stream classes, starting with FileInputStream and FileOutputStream, where we will see practical examples of reading from and writing to files in Java.