Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Io Java IO FileReader

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO FileReader

Advertisement
In this tutorial we will learn about the FileReader class in Java.

Java IO FileReader

In this tutorial we will learn about the FileReader class in Java.

java.io.FileReader class is used to read the character streams. To read the streams FileInputStream can be considered. This class extends the java.io.InputStreamReader.

Constructor of FileReader

Constructor Name
Description
FileReader(File file) This constructor creates a FileReader which contains a reference of File to read the stream.
Syntax : public FileReader(File file) throws FileNotFoundException
FileReader(FileDescriptor fd) This constructor creates a FileReader which contains a reference of FileDescriptor to read the stream.
Syntax : public FileReader(FileDescriptor fd)
FileReader(String fileName) This constructor creates a FileReader which contains the name of a file to read the stream.
Syntax : public FileReader(String fileName) throws FileNotFoundException

Methods in this class are inherited from its super classes.

Example :

Here I am giving a simple example which will demonstrate you about how to use java.io.FileReader. In this example I have created a Java class named JavaFileReaderExample.java where created a FileReader using the constructor FileReader(String fileName) and created a file named file.txt and saved it to the same folder where this Java class is saved. Contents of this file is treated as stream of raw bytes. Then tried to read data from the file using read() method inherited from its super class. And then simply display the read streams from the file to the console.

Source Code

import java.io.Reader;
import java.io.FileReader;
import java.io.IOException;

public class JavaFileReaderExample
{
  public static void main(String args[])
   {
      FileReader fr = null;      
      
     try
       {
          fr = new FileReader("file.txt");
          int c ;
          System.out.println();
          System.out.println("***** OUTPUT *****");
          System.out.println();
          while((c= fr.read()) != -1)
            {
               System.out.print((char) c);
            }
          System.out.println();
       }
      catch(Exception ex)
       {
          System.out.println(ex);
       }
      finally
       {
          if(fr != null)
            {
               try
                 {
                    fr.close();
                 }
               catch(IOException ioe)
                 {
                     System.out.println(ioe);
                 }
            }
       }// end finally
   }// end main
}// end class

Outupt

When you will execute the above example as given in the output you will get the output as follows :

Source code of this example can be downloaded from the link given below. The downloaded file will contained the text file and the java file which contains the actual code.

Download Source Code

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: December 14, 2012

Related Tutorials

Discuss: Java IO FileReader  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com