< Prev
Next >



FileInputStream



FileInputStream class is a subclass of InputStream abstract class. FileInputStream is used create an input stream, which is used to read byte/bytes from a file.




Constructors :


1) FileInputStream(File file)
This constructor creates a FileInputStream object to read a file specified by the File object, which is passed to this constructor as a parameter.
Example -
File file= new File("D:\\Textbook.txt");
FileInputStream fis= new FileInputStream(file); 
2) FileInputStream(String path)
This constructor creates a FileInputStream to read a file which is accessed by the path mentioned in the parameters of this constructor.
Example -
FileInputStream fis= new FileInputStream("D:\\TextBook.txt");
Both constructors have created a FileInputStream object to create an input stream to read a file called "TextBook.txt" which is located in the D drive.




Some methods for reading FileInputStream


Methods Description
available()
This method gives the remaining number of bytes available to read from this input stream.

read()
This method reads one byte from this input stream.

read(byte[] b)
This method reads a whole byte array from this input stream.

close()
This method closes this input stream and also frees any resources connected with this input stream.






Reading one byte at a time out of a file using read() method of FileInputStream


We are reading an existing file called "TextBook.txt" using its path "D:\\TextBook.txt". Let's say the contents of this file "TextBook.txt" are -
 
 World peace is extremely important. 
 
//Program to read one byte at a time through an existing file using FileInputStream.

import java.io.*;

class A
{
public static void main(String...ar)
{
try
{
FileInputStream fin= new FileInputStream("D:\\TextBook.txt");
int c;

while( (c=fin.read()) !=-1) //Reading one byte out of a file
{
System.out.print((char)c); //casting byte to char for displaying on the screen using, System.out
}

fin.close();
}

catch(IOException e)
{
System.out.println(e);
}

}
}



Output is -:


World peace is extremely important


Program Analysis







Reading a whole byte array out of a file using read(byte[] b) method.


Reading the contents of the same file TextBook.txt, but this time we are reading a whole byte array at once for faster reading.
 //Program to read a byte array out of an existing file using FileInputStream.
 
import java.io.*;

class A
{
public static void main(String...ar)
{
try
{
	FileInputStream fin= new FileInputStream("D:\\TextBook.txt");
	int num= fin.available();	
	System.out.println("Number of bytes available to read =" + num);
	
	byte b[]= new byte[num];
	
	int c;
	if((c=fin.read(b))!=-1) //read() method stops reading from file when EOF is reached i.e. -1	
	System.out.println("Number of bytes read = " +c);

	String s= new String(b);
	System.out.println(s);
	fin.close();

}
catch(IOException e)
{
	System.out.println(e);
}

}
}



Output is -:


Number of bytes available to read =34
Number of bytes read = 34
World peace is extremely important


Program Analysis






Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< What is File?
FileOutputStream >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2