- File Class is used for file operation like creating a file, reading a file, updating a file, deleting a file, creating of directories, file search operations.
- FileInputStream Class used for reading bytes from a file in a file system. This Class is useful for reading the stream of bytes, raw data like image& etc. FileReader Class is useful to read the stream of characters.
- BufferedInputStream Class is extended with FileInputStream& added with additional functionality of store stream in a Buffer. With Buffer, we can do the operations like read stream, skip the bytes, mark, reset, update the stream as needed, ready with next byte and updating the stream as needed.
Read File Using Java BufferedInputStream Example.
This example shows how to read a file using available and read methods of Java BufferedInputStream.
package snippets11;
import java.io.*;
/**
* Created by root on 15/3/16.
*/
public class ReadFileUsingBufferedInputStream {
public static void main(String[] args) {
File file=null;
FileInputStream in=null;
BufferedInputStream bf=null;
try {
file=new File("/home/suresh/temp/testjava.txt");
in=new FileInputStream ( file );
bf=new BufferedInputStream ( in );
/*
* BufferedInputStream has ability to buffer input into
* internal buffer array.
*
* available() method returns number of bytes that can be
* read from underlying stream without blocking.
*/
//read file using BufferedInputStream
while (bf.available ()>0){
System.out.print((char)bf.read());
}
} catch (FileNotFoundException e) {
e.printStackTrace ( );
} catch (IOException e) {
e.printStackTrace ( );
}finally {
//close the BufferedInputStream using close method
try{
if(bf != null)
bf.close();
}catch(IOException ioe)
{
System.out.println("Error while closing the stream : " + ioe);
}
}
}
}