Read File Using Java BufferedInputStream Example

  1. /*
  2. Read File Using Java BufferedInputStream Example.
  3. This example shows how to read a file using available and read
  4. methods of Java BufferedInputStream.
  5. */
  6.  
  7. import java.io.*;
  8.  
  9. public class ReadFileUsingBufferedInputStream {
  10.  
  11. public static void main(String[] args) {
  12.  
  13. //create file object
  14. File file = new File("C://FileIO//ReadFile.txt");
  15. BufferedInputStream bin = null;
  16.  
  17. try
  18. {
  19. //create FileInputStream object
  20. FileInputStream fin = new FileInputStream(file);
  21.  
  22. //create object of BufferedInputStream
  23. bin = new BufferedInputStream(fin);
  24.  
  25. /*
  26. * BufferedInputStream has ability to buffer input into
  27. * internal buffer array.
  28. *
  29. * available() method returns number of bytes that can be
  30. * read from underlying stream without blocking.
  31. */
  32.  
  33. //read file using BufferedInputStream
  34. while( bin.available() > 0 ){
  35.  
  36. System.out.print((char)bin.read());
  37. }
  38.  
  39. }
  40. catch(FileNotFoundException e)
  41. {
  42. System.out.println("File not found" + e);
  43. }
  44. catch(IOException ioe)
  45. {
  46. System.out.println("Exception while reading the file " + ioe);
  47. }
  48. finally
  49. {
  50. //close the BufferedInputStream using close method
  51. try{
  52. if(bin != null)
  53. bin.close();
  54.  
  55. }catch(IOException ioe)
  56. {
  57. System.out.println("Error while closing the stream : " + ioe);
  58. }
  59.  
  60.  
  61. }
  62. }
  63.  
  64. }
  65.  
  66.  
  67. /*
  68. Output would be
  69. This file is for demonstration of how to read a file using Java
  70. BufferedInputStream.
  71. */

Vertical Output

This program shows output vertically as compared to the standard way of displaying file contents

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!