Read File char by char with BufferedInputStream in Java

Description

The following code shows how to read File char by char with BufferedInputStream.

For faster reading you should use BufferedInputStream to wrap any InputStream. BufferedInputStream has two constructors that accept an InputStream:


public BufferedInputStream(InputStream in)
public BufferedInput Stream (Input Stream in, int bufferSize)

For example, the following code wraps a FileInputStream with a BufferedInputStream.


FileInputStream fis = new FileInputStream(aFile);
BufferedInputStream bis = new BufferedInputStream (fis);

Then, instead of calling the methods on fis, work with the BufferedInputStream bis.

Example


//www  . j  a  v  a 2  s.c om
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

public class Main {

  public static void main(String[] args) throws Exception {
    File file = new File("C:/ReadFile.txt");
    FileInputStream fin = new FileInputStream(file);

    BufferedInputStream bin = new BufferedInputStream(fin);
    while (bin.available() > 0) {
      System.out.println((char) bin.read());
    }
    bin.close();
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip