There are many more methods of File Input Stream class which are also very helpful in getting data from a file; some of them are int read(byte[] b), this function reads data from the input stream up to b.length bytes in length. File Channel gets the channel(): The specific File Channel object connected to the file input stream is returned using it. Finalize() is used to ensure that the close() function is invoked when there is no longer reference to the file input stream.”
Example 01: Reading a Single Byte From a Text File Using the read() and close() Methods of the Input Stream Class
This example uses File Input Stream to read a single character and print out the content. Suppose we have a file named “file.txt” with the content shown below:
Suppose we have a file named “file.txt” with the content shown above. Let us now attempt to read and print the first character of the file.
We must first import java.io. File Input Stream package in order to construct a file input stream. Then we will create a new object of File Input Stream that will be linked to the file specified (file.txt) in variable “f”.
In this example, we will be using the “int read()” method of the Java File Input Stream class, which is used to read a single byte from the file and save it in the variable “I”. Next, the “System.out.print(char(i))” displays the character corresponding to that byte.
f.close() method closes the file and stream.We will obtain the following output after building and running the aforesaid script, as we can see only the initial letter of the text “L” is printed.
Example 02: Reading All the Content of a Text File Using the read() and close() Methods of the Input Stream Class
In this example, we will be reading and displaying all the contents of a text file; as shown below:
Once again, we will be importing java.io. File Input Stream package in order to construct a file input stream.
First, we will read the first byte of the file and display the corresponding character inside the while loop. The while loop will run until there are no bytes left, that is, the end of the text in the file. Line 12 will read the next byte, and the loop will continue until the last byte of the file.
After compiling and executing the above code, we will get the following results. As we can see, the whole text of the file “Lorep Ipsum” is displayed in the terminal.
Example 03: Determining the Number of Available Bytes in a Text File by Using the available() Method of Input Stream Class
In this example, we will use the File Input Stream’s “available()” function to determine the number of existing bytes in the file input stream.
First, we generated an object of file input stream class named “a” with the following code. In line 5, we utilized the “available()” method to determine and display the total amount of available bytes in the file. Then from line 6 to line 8, we used the “read()” function thrice. Now in line 9, we used the “available()” method again to check and display the remaining bytes.
After compiling and running the code, we can see the first line of the output shows the total number of available bytes in the file. The next line shows the number of bytes available at the end of the code, which is 3 less than the bytes available at the beginning. This is because we used the read method thrice in our code.
Example 04: Skipping Bytes of a Text File to Read Data From a Specific Point Using the skip() Method of Input Stream Class
In this example, we will be using the “skip(x)” method of File Input Stream, which is used to ignore and disregard the given number of bytes of data from the input stream.
In the code below, first, we have created a file input stream and stored it in the variable “a”. Next, we have used the “a.skip(5)” method, which will skip the first 5 bytes of the file. Next, we printed the remaining characters of the file using the “read()” method inside a while loop. Finally, we closed the file input stream by the “close()” method.
Below is the screenshot of the terminal after compiling and running the code. As we can see, only “Ipsum” is displayed as we have skipped the first 5 bytes using the “skip()” method.
Conclusion
In this article, we have discussed the uses of the File Input Stream class and its different methods; read(), available(), skip(), and close(). We used these methods to read the first element of a file by using the read() and close() methods. Then we read the whole file through the iterative approach and using the same methods. Then we used the available() method to determine the number of bytes present at the commencement and completion of the file. After that, we used the skip() method to skip several bytes before reading the file, which allowed us to get the specific data that we needed.