FileInputStream
Java FileInputStream Class
Last Updated : 17 Mar 2025
Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented data (streams of raw bytes) such as image data, audio, video etc. You can also read character-stream data. But, for reading streams of characters, it is recommended to use FileReader class.
Java FileInputStream Class Declaration
Let's see the declaration for java.io.FileInputStream class:
Java FileInputStream Class Methods
| Method | Description |
|---|---|
| int available() | It is used to return the estimated number of bytes that can be read from the input stream. |
| int read() | It is used to read the byte of data from the input stream. |
| int read(byte[] b) | It is used to read up to b.length bytes of data from the input stream. |
| int read(byte[] b, int off, int len) | It is used to read up to len bytes of data from the input stream. |
| long skip(long x) | It is used to skip over and discards x bytes of data from the input stream. |
| FileChannel getChannel() | It is used to return the unique FileChannel object associated with the file input stream. |
| FileDescriptor getFD() | It is used to return the FileDescriptor object. |
| protected void finalize() | It is used to ensure that the close method is call when there is no more reference to the file input stream. |
| void close() | It is used to closes the stream. |
Java FileInputStream Example 1: Read Single Character
File Name: DataStreamExample.java
Note: Before running the code, a text file named as "testout.txt" is required to be created. In this file, we have written the following content in the file:
Welcome to javatpoint.
After executing the above program, we will get a single character from the file which is 87 (in byte form). To see the text, we need to convert it into character.
Output:
W
Java FileInputStream Example 2: Read All Characters
File Name: DataStreamExample.java
Output:
Welcome to javaTpoint
FileInputStream Class Constructors
The FileInputStream class in Java provides several constructors to create a FileInputStream instance, allowing us to read bytes from a file in various ways. Here is an overview of the constructors available in the FileInputStream class:
1. FileInputStream(String name)
It creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.
- Parameters: name - the name of the file.
- Throws FileNotFoundException if the file does not exist, is a directory rather than a regular file, or for some other reason, cannot be opened for reading.
Example:
File Name: FileInputStreamExample.java
Output:

2. FileInputStream(File file)
It creates a FileInputStream by opening a connection to an actual file represented by a File object.
- Parameters: file - the File object that represents the file to be opened.
- Throws FileNotFoundException if the file does not exist, is a directory rather than a regular file, or for some other reason, cannot be opened for reading.
Example:
File Name: FileInputStreamExample.java
Output:

3. FileInputStream(FileDescriptor fdObj)
It creates a FileInputStream by using a file descriptor fdObj that represents an existing connection to an actual file in the file system.
- Parameters: fdObj - the file descriptor.
- Note: It is useful when we have a FileDescriptor for a file, perhaps obtained from another file system operation or as part of inter-process communication.
Example:
File Name: FileDescriptorExample.java
Output:
Hello, world!

The code will create a file called example.txt, write "Hello, world!" to it, and then read it back using a FileDescriptor to demonstrate how you can utilize this Java feature. Adjust the file path and environment according to your needs. It is a self-contained example suitable for understanding how FileInputStream and FileDescriptor work together in practice.
Next TopicJava BufferedOutputStream Class