Java BufferedInputStream

BufferedInputStream通過緩衝數據向輸入流添加功能。它維護一個內部緩衝區以存儲從底層輸入流讀取的字節。創建緩衝區輸入流如下:

String srcFile = "test.txt"; 
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));

以下代碼顯示如何使用BufferedInputStream從文件讀取數據。

import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class Main {
  public static void main(String[] args) {
    String srcFile = "test.txt";
    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
        srcFile))) {
      // Read one byte at a time and display it
      byte byteData;
      while ((byteData = (byte) bis.read()) != -1) {
        System.out.print((char) byteData);
      }
    } catch (Exception e2) {
      e2.printStackTrace();
    }
  }
}

上面的代碼生成以下結果。

Yiibai.com BufferedInputStream Usge.
The Line 2..