Java Inputstream Class
InputStream is an abstract base class in the Java I/O system for reading byte data, located in the `java.io` package.
InputStream is the parent class of all byte input streams, providing a basic framework for reading byte data.
InputStream is an abstract class, which means you cannot instantiate it directly, but you can create specific input stream objects through its subclasses (such as FileInputStream, ByteArrayInputStream, etc.).
* * *
## Core Methods Analysis
InputStream is part of Java IO streams, used for reading data in byte (8-bit) units.
InputStream is suitable for handling non-text type data, such as images, audio, video, compressed files, etc.
You can think of it as a "water pipe", where data flows like water from its source into your program.
!(#)
### Core Methods
| Method Name | Description |
| --- | --- |
| `int read()` | Reads one byte, returns an int value between 0~255, returns `-1` if end of stream is reached |
| `int read(byte[] b)` | Reads up to `b.length` bytes of data from the stream into byte array `b` |
| `int read(byte[] b, int off, int len)` | Reads up to `len` bytes starting at offset `off` into array `b` |
| `void close()` | Closes the stream and releases associated resources |
### 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.
#### Sample Code
## Example
InputStream input =new FileInputStream("example.txt");
int data = input.read();
while(data !=-1){
System.out.print((char)data);
data = input.read();
}
input.close();
### Reading Byte Array
## Example
public int read(byte[] b)throws IOException
This method attempts to read up to 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.
#### Sample Code
## Example
InputStream input =new FileInputStream("example.txt");
byte[] buffer =new byte;
int bytesRead;
while((bytesRead = input.read(buffer))!=-1){
System.out.println("Read "+ bytesRead +" bytes");
}
input.close();
### Reading Specified Length of Bytes
## Example
public int read(byte[] b, int off, int len)throws IOException
This method allows you to specify where to start storing the read data (off) and the maximum number of bytes to read (len).
#### Sample Code
## Example
InputStream input =new FileInputStream("example.txt");
byte[] buffer =new byte;
int bytesRead = input.read(buffer, 10, 500);// Start storing from buffer, read up to 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.
* * *
## Common Subclasses Introduction
* **FileInputStream**: Used for reading data from files.
* **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 that were previously serialized using ObjectOutputStream.
* **PipedInputStream**: Used with PipedOutputStream to implement pipe communication between threads.
* * *
## 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(Input
YouTip