Skip to content

How to read file in Java – BufferedInputStream

Ramesh Fadatare edited this page Jul 18, 2018 · 3 revisions

1. Overview

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.

2. Read File using BufferedInputStream Example

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();
		}
	}
}

3. Reference

https://docs.oracle.com/javase/8/docs/api/java/io/BufferedInputStream.html https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Clone this wiki locally