How to read file in Java – BufferedInputStream

1. Overview

In this example, we will use a BufferedInputStream class to read a file. The BufferedInputStream class is used to read information from the stream. It internally uses a 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

Comments