Home Java Example Java Io Read the File

Related Tutorials


 
 

Share on Google+Share on Google+

Read the File

Advertisement
As we have read about the BufferedInputStream class that lets you read characters from a stream and stores it in an internal buffer.

Read the File

     

As we have read about  the BufferedInputStream class that lets you read characters from a stream and stores it in an internal buffer. Lets see an example that reads the contains of the existed file.

The given example uses the BufferedInputstream class that reads the bytes from the input stream. The input stream is a file "Filterfile.txt" from which the data is read in form of a byte through the read( ) method. The read( ) method reads the byte from the file that is converted into the character-form.

 

import java.io.*; 

class ReadFilter
{
  public static void main(String args[])
  {
  try 
  {
  FileInputStream fin = new FileInputStream("Filterfile.txt");
  BufferedInputStream bis = new BufferedInputStream(fin);
  
  // Now read the buffered stream.
  while (bis.available() 0
  {
  System.out.print((char)bis.read());
  }
  
  
  catch (Exception e
  {
  System.err.println("Error reading file: " + e);
  }
  }
}

Output of the Program:

C:\nisha>javac ReadFilter.java

C:\nisha>java ReadFilter
This is a text file
C:\nisha>

Download this Program

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2009

Related Tutorials

Discuss: Read the File  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
vivekbasapur
June 20, 2011
bufferd class

sir i need the syntax for bufferd class
Anonymous
July 28, 2013
Closing

You're not even closing the file streams and file.
DMCA.com