Java BufferedInputStream class

Introduction

Buffering I/O is a performance optimization.

Java's BufferedInputStream can wrap any InputStream into a buffered stream to improve performance.

BufferedInputStream has two constructors:

BufferedInputStream(InputStream inputStream)  
BufferedInputStream(InputStream inputStream, int bufSize) 

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class Main {
   public static void main(String args[]) {
      String s = "This is a test from demo2s.com\n";
      byte buf[] = s.getBytes();

      ByteArrayInputStream in = new ByteArrayInputStream(buf);
      int c;/*from  w  w w.  j av a  2  s  .c om*/
      // Use try-with-resources to manage the file.
      try (BufferedInputStream f = new BufferedInputStream(in)) {
         while ((c = f.read()) != -1) {
             System.out.print((char) c);
         }
      } catch (IOException e) {
         System.out.println("I/O Error: " + e);
      }
   }
}



PreviousNext

Related