Home  >  Core Java

java.io.BufferedInputStream Example in Java

By Arvind Rai, September 27, 2013
java.io.BufferedInputStream is used to create internal buffer array and then the bytes are read. BufferedInputStream takes the input as InputStream to process it. BufferedInputStream can be used to read a file from file system. In our example, we are reading a file and displaying in into console.
C:\test.txt
 
The above file contains text as Hello World!

BufferedInputStreamDemo.java
package com.concretepage.io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInputStreamDemo {
  public static void main(String[] args) {
      File file = new File("C:\\test.txt");
      BufferedInputStream bis = null;
      try {
        bis = new BufferedInputStream(new FileInputStream(file));
        while (bis.available() != 0) {
            System.out.print(new Character((char)bis.read()));
          }
        
      } catch (IOException e) {
         e.printStackTrace();
      }finally {
          try {
              bis.close();
            } catch (IOException ex) {
              ex.printStackTrace();
            }
          }
   }
}
 
Output:
Hello World!
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us