Java 緩沖輸入流

2018-02-06 19:21 更新

Java IO教程 - Java緩沖輸入流


BufferedInputStream通過緩沖數(shù)據(jù)向輸入流添加功能。

它維護(hù)一個(gè)內(nèi)部緩沖區(qū)以存儲從底層輸入流讀取的字節(jié)。

我們創(chuàng)建緩沖區(qū)輸入流如下:

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

上面的代碼生成以下結(jié)果。



以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號