Java enables the developers to work with the files. In such a case, the “InputStream” class comes into effect that assists in reading the specified file contents along with analyzing the contained bytes. This results in coping with the memory issues and also returning the selective file content as per the requirement efficiently.
This write-up will elaborate on the working of the “InputStream” in Java.
How Does the Java InputStream Work?
The “InputStream” class of the “java.io” package corresponds to an abstract superclass that returns an input stream comprising of bytes against the specified file.
Subclasses of InputStream
In order to apply the “InputStream” functionalities, its following subclasses can be utilized:
-
- FileInputStream
- ObjectInputStream
- ByteArrayInputStream
It is such that these subclasses extend the “InputStream” class.
Methods of InputStream
The “InputStream” class contains different methods that are applied by its subclasses. Following are some of the most commonly used methods:
Methods | Functionality |
read() | It reads a byte of data from the input stream.
|
read(byte[] array) | It also reads bytes from the stream and stores them in the target array.
|
skip() | This method skips/omits the specific number of bytes from the input stream.
|
available() | It gives the contained bytes in the input stream. |
reset() | It gives access to the stream point where the mark was set.
|
mark() | This method marks the position in the stream till which data has been read.
|
markSupported() | It analyzes if the “mark()” and “reset()” methods are supported/compatible in the stream.
|
Before heading to the example, import the following packages to work with the “InputStream” and its subclass:
import java.io.InputStream;
Example: Working of InputStream in Java
This example illustrates the working of the “InputStream” by reading the file content via the “InputStream” methods:
public static void main(String args[]) {
byte[] givenarray = new byte[50];
try {
InputStream readData = new FileInputStream("readfile.txt");
System.out.println("Bytes in the file -> " + readData.available());
readData.read(givenarray);
System.out.println("Read File Data: ");
String containdata = new String(givenarray);
System.out.println(containdata);
readData.close();
}
catch (Exception except) {
except.getStackTrace();
}
}}
According to the above code lines, perform the below-stated steps:
-
- First, create a “byte” array that can comprise a maximum of “50” byte values in the read file.
- In the next step, create an “InputStream” using “FileInputStream” and return the available bytes in the specified file via the associated “available()” method.
- After that, read the bytes from the input stream using the “read()” method.
- Now, convert the byte array into a string and display the file contents.
- Lastly, close the read file using the associated “close()” method.
Output
In this outcome, it can be implied that the number of bytes in the file i.e., accumulated space by the content is returned in accordance with the max range in the byte array i.e., “50”. Also, the file content is returned appropriately.
File Content
To get an overview of the “skip()” and “reset()” methods, consider the following demonstration:
In this illustration, the “skip()” method skips the specified number of bytes i.e., “5 -> Java” from the file content from the start. The “reset()” method however resets the stream.
Conclusion
The “InputStream” class of the “java.io” package is an abstract superclass that corresponds to an input stream of bytes used for reading the file data. This class comprises various methods that assist in the reading methodologies of the file effectively as per the requirement. This blog demonstrated the purpose and working of the “InputStream” in Java.