English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn Java FileInputStream and its methods with the help of examples.
The java.io package's FileInputStream class can be used to read data from a file (in bytes).
It inherits the InputStream abstract class.
Before learning FileInputStream, make sure you understandJava File (file).
To create a file input stream, we must first import the java.io.FileInputStream package. After importing the package, we can use Java to create a file input stream.
1Using the file path
FileInputStream input = new FileInputStream(stringPath);
Here, we create an input stream that will be linked to the specified file path.
2Using the file object
FileInputStream input = new FileInputStream(File fileObject);
Here, we create an input stream that will be linked to the file specified by fileObject.
The FileInputStream class provides implementations for different methods that appear in the InputStream class.
read() - Reads a byte from the file
read(byte[] array) - Reads bytes from the file and stores them in the specified array
read(byte[] array, int start, int length) - Reads a number of bytes equal to length from the file and stores them in the specified array starting from position start
Suppose we have a file namedinput.txtThe file contains the following content.
This is a line of text inside the file.
Let's try to read this file using FileInputStream.
import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { FileInputStream input = new FileInputStream("input.txt"); System.out.println("File data: "); //Read the first byte int i = input.read(); while(i != -1) { System.out.print((char)i); //Read the next byte from the file i = input.read(); } input.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Result
Data in the file: This is a line of text inside the file.
In the above example, we created a file input stream named input. The input stream is associated withinput.txtfile link.
FileInputStream input = new FileInputStream("input.txt");
To read data from the file, we used the read() method in the while loop.
To get the number of available bytes, we can use the available() method. For example,
import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { //Assuming the input.txt file contains the following text //This is a line of text in the file. FileInputStream input = new FileInputStream("input.txt"); //Returns the number of available bytes System.out.println("Available bytes at the beginning: "); + input.available()); //Read from the file3bytes input.read(); input.read(); input.read(); //Returns the number of available bytes System.out.println("Available bytes at the end: "); + input.available()); input.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output Result
Available bytes at the beginning: 39 Available bytes at the end: 36
In the above example,
We first use the available() method to check the number of available bytes in the file input stream.
Then, we have used the read() method3time from the file input stream3bytes.
Now, after reading the bytes, we have checked the available bytes again. This time, the available bytes have decreased by3.
To discard and skip a specified number of bytes, you can use the skip() method. For example
import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { //Assuming the input.txt file contains the following text //This is a line of text in the file. FileInputStream input = new FileInputStream("input.txt"); //skip5bytes input.skip(5); System.out.println("Input stream skips5bytes: "); //Read the first byte int i = input.read(); while (i != -1) { System.out.print((char) i); //Read the next byte from the file i = input.read(); } // close() method input.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output Result
Input Stream Skip5bytes: is a line of text inside the file.
In the above example, we used the skip() method to skip5bytes of data. Therefore, text representation will not be read from the input stream“This”bytes.
To close the file input stream, you can use the close() method. Once the close() method is called, we cannot use the input stream to read data.
In all the above examples, we have used the close() method to close the file input stream.
Method | Content Description |
---|---|
finalize() | To ensure that the close() method is called |
getChannel() | To return the object associated with the FileChannel and the input stream |
getFD() | To return the file descriptor associated with the input stream |
mark() | To mark the position of the data that has been read in the input stream |
reset() | To return the control to the point in the input stream where marking was set |