1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
/* Read File Using Java BufferedInputStream Example. This example shows how to read a file using available and read methods of Java BufferedInputStream. */ import java.io.*; public class ReadFileUsingBufferedInputStream { public static void main(String[] args) { //create file object File file = new File("C://FileIO//ReadFile.txt"); BufferedInputStream bin = null; try { //create FileInputStream object FileInputStream fin = new FileInputStream(file); //create object of BufferedInputStream bin = new BufferedInputStream(fin); /* * BufferedInputStream has ability to buffer input into * internal buffer array. * * available() method returns number of bytes that can be * read from underlying stream without blocking. */ //read file using BufferedInputStream while( bin.available() > 0 ){ System.out.print((char)bin.read()); } } catch(FileNotFoundException e) { System.out.println("File not found" + e); } catch(IOException ioe) { System.out.println("Exception while reading the file " + ioe); } finally { //close the BufferedInputStream using close method try{ if(bin != null) bin.close(); }catch(IOException ioe) { System.out.println("Error while closing the stream : " + ioe); } } } } /* Output would be This file is for demonstration of how to read a file using Java BufferedInputStream. */ |
This program shows output vertically as compared to the standard way of displaying file contents
marvelous examples for beginners as wel as for experts
I like this.It is easy to understand the basic concepts.
Good Examples to Understand the Basic Concepts.. 🙂
got error :
Error while creating a new empty file :java.io.IOException: Access is denied
Was file C:\demo123.txt created ? : false
why?
In the finally why we are using two catch
Great job, Good Examples.
.