Java FileInputStream

In this section we will discuss about the Java IO FileInputStream.

Java FileInputStream

In this section we will discuss about the Java IO FileInputStream.

Java FileInputStream

Java FileInputStream

In this section we will discuss about the Java IO FileInputStream.

FileInputStream is a class provided in the java.io package for reading the input bytes from a file. An InputStream is a super class of FileInputStream class.

FileInputStream's object can be created using following its constructor

Constructor Name Description
FileInputStream(File file) A FileInputStream is created for the specified file created using the File object.
FileInputStream(FileDescriptor fdObj) A FileInputStream is created for the specified file descriptor.
FileInputStream(String name) A FileInputStream is created for the specified file path name.

Commonly used methods of FileInputStream

Method Name Description
available() This method is used to get the number of bytes which are not read yet and can be read or skipped over from the input stream.
Syntax : public int available() throws IOException
close() This method is used to close the input stream and release the system resource if any, associated with the stream.
Syntax : public void close() throws IOException
getFD() This method is used to get the FileDescriptor object.
Syntax : public final FileDescriptor getFD() throws IOException
read() This method is used to read byte of data from the current input stream.
Syntax : public int read() throws IOException
read(byte[] b) This method is used to read byte of data from the specified length of array.
Syntax : public int read(byte[] b) throws IOException
read(byte[] b, int off, int len) This method is used to read specified number of byte of data from the specified array, started with the specified offset value.
Syntax : public int read(byte[] b, int off, int len) throws IOException
skip(long n) This method is used to skipped over the specified number of bytes.
Syntax : public long skip(long n) throws IOException

Example :

In this example I have created a class named ReadFileInputStream.java into which created an object of FileInputStream using FileInputStream(String name). In the argument of this constructor I have provided a name of file which I have already created in the current directory. If the file is located outside the current directory then give the complete path of file. Using the read() method of FileInputStream read the text of the text file.

Source Code

/* This example demonstrate that how
FileInputStream reads the bytes of data
from the file.
*/

import java.io.FileInputStream;
import java.io.IOException;
public class ReadFileInputStream
{
public static void main(String args[])
{
FileInputStream fis = null; 
try
{
fis = new FileInputStream("abc.txt");
int r;
System.out.println("******* Contents of abc.txt file ***********");
System.out.println();
while((r= fis.read()) != -1)
{
System.out.print((char) r);
}
System.out.println();
}
catch(IOException e)
{
System.out.println("IOException caught..!!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
try
{
fis.close();
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
}
}

When you will execute the above code you will get the output as follows :

Download Source Code