-
Notifications
You must be signed in to change notification settings - Fork 8
How to read file in Java – BufferedInputStream
Ramesh Fadatare edited this page Jul 18, 2018
·
3 revisions
In this example, we will use BufferedInputStream class to read file. The BufferedInputStream class is used to read information from stream. It internally uses buffer mechanism to make the performance fast.
package com.javaguides.javaio.fileoperations.examples;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* This Java program demonstrates how to read file in Java using BufferedInputStream.
* @author javaguides.net
*/
public class BufferedInputStreamExample {
public static void main(String[] args) {
try( FileInputStream fin=new FileInputStream("D:\\testout.txt");
BufferedInputStream bin=new BufferedInputStream(fin); ){
int i;
while((i=bin.read())!=-1){
System.out.print((char)i);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
https://docs.oracle.com/javase/8/docs/api/java/io/BufferedInputStream.html https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html