Signup/Sign In

Java FileInputStream Class

In this tutorial, we will learn about FileInputStream and its various methods in Java. This class reads a stream of bytes from the file and belongs to java.io package.

Syntax

We can see the syntax declaration of FileInputStream class, by default it inherits InputStream class and it means we can call all the methods of this class with the object of FileInputStram class.

public class FileInputStream extends InputStream

Methods

In the table given below methods of FileInputStream class and their uses are given:

Method Description
int available() This method is used to return the estimated number of bytes that can be read from the input stream.
int read() This method is used to read the byte of data from the input stream.
int read(byte[] b) This method is used to read up to b.length bytes of data from the input stream.
int read(byte[] b, int off, int len) This method is used to read up to len bytes of data from the input stream.
long skip(long x) This method is used to skip over and discards x bytes of data from the input stream.
FileChannel getChannel() This method is used to return the unique FileChannel object associated with the file input stream.
FileDescriptor getFD() This method is used to return the FileDescriptor object.
protected void finalize() This method is used to ensure that the close method is called when there is no more reference to the file input stream.
void close() This method is used to closes the stream.

Example of FileInputStream to read data from file

The example given below will read data from myfile.txt with the help of FileInputStream class and it's read() method. read() method reads only a single byte at a time, therefore, we are calling in inside the loop so it can read all the characters. This will return -1 at the end of the file and there we will stop.

import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{  
		try
		{    
			FileInputStream fin=new FileInputStream("E:\\studytonight\\myfile.txt");    
			int i=0;    
			while((i=fin.read())!=-1){    
				System.out.print((char)i);    
			}      
			fin.close();  
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}    
	}  
}


Hello studytonight

Example of available() Method

In the program given below, we are implementing an available() method to check the number of bytes available in the input source to read. As we can when we are calling the available() method for the first time it is showing 18 bytes are available to read. Now we call the read() function twice so it will reduce the number of bytes to read from the file, so it will print the 16 available bytes. We can use the available method to check the number of available bytes to read from the input source.

import java.io.FileInputStream;
public class StudyTonight
{
	public static void main(String args[])
	{
		try 
		{
			FileInputStream input = new FileInputStream("E:\\studytonight\\file.txt");
			System.out.println("Available bytes = " + input.available());
			input.read();
			input.read();
			System.out.println("Available bytes = " + input.available());
			input.close();
		}
		catch (Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}

.


Available bytes = 18
Available bytes = 16

file.txt

Hello Studytonight

Example of the skip() Method

In the given program we are implementing skip() method. This method takes a parameter as a number of bytes to be skipped from the input stream. In the following program, we are calling the skip method by passing 5 as a parameter so it will skip the first 5 bytes from the input stream so when we are read the data from this stream it will return the data after first 5 bytes. For example in the given file we have a data "Hello Studytonight" and we skipped the first 5 bytes so it will directly print " Studytonight"

import java.io.FileInputStream;
public class StudyTonight
{
	public static void main(String args[])
	{
		try 
		{
			FileInputStream input = new FileInputStream("E:\\studytonight\\file.txt");
			input.skip(5);
			int ch=0;
			while(ch!=-1)
			{
				System.out.print((char)ch);
				ch=input.read();
			}		
			input.close();
		}
		catch (Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}


Studytonight

file.txt

Hello Studytonight

Conclusion

In this tutorial, we learned how to read a stream of a byte from the given file. To read data from the file we used FileInputStream class.

The read() method reads a byte by byte from the file and this way we read the whole data from a file.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.