The FileInputStream class is specially designed to work with byte – oriented input from the files. The FileInputStream is derived from abstract class InputStream. FileInputStream class can throw FileNotFoundException when its unable find input file.
Let us look a simple example of FileInputStream to read byte streams from the file as illustrated below.
Example:
//From FileInputStreamExample.java
import java.io.FileInputStream;
/**
*
* @author JavaHotSpot
*/
public class fileInputStreamExample
{
public static void main(String args[])
{
try
{
int size;
FileInputStream fi = new FileInputStream("C:\\file.txt");
size=fi.available();
System.out.println("The available bytes ="+size);
byte[] bt=new byte[1000];
fi.read(bt);
System.out.println("File Content is =\n"+ new String(bt,0,size));
fi.close();
}
catch(Exception ex)
{
System.err.println(ex);
}
}
}
Output:
The available bytes =177
File Content is =
A stream represents a flow of data,
or a channel of communication with a
writer at one end and a reader at the other.
To handle input and output we use java.io package.
Related Articles:
FileOutputStream
InputStreamReader
FileReader
FileWriter
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
DataInputStream
DataOutputStream
ObjectInputStream
ObjectOutputStream
Related Articles:
FileOutputStream
InputStreamReader
FileReader
FileWriter
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
DataInputStream
DataOutputStream
ObjectInputStream
ObjectOutputStream