FileReader 

Introduction

The Java.io.FileReader class is a convenience class for reading character files.Following are the important points about FileReader −

  • The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.
  • FileReader is meant for reading streams of characters. For reading streams of raw bytes, use FileInputStream.

Class declaration

Following is the declaration for Java.io.FileReader class −

publicclassFileReaderextendsInputStreamReader

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

Field

Following are the fields for Java.io.FileReader class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No.Constructor & Description
1FileReader(File file)This constructor creates a new FileReader, given the File to read from.
2FileReader(FileDescriptor fd)This constructor creates a new FileReader, given the FileDescriptor to read from.
3FileReader(String fileName)This constructor creates a new FileReader, given the name of the file to read from.

Once you have FileReader object in hand then there is a list of helper methods which can be used to manipulate the files.

Sr.No.Method & Description
1public int read() throws IOExceptionReads a single character. Returns an int, which represents the character read.
2public int read(char [] c, int offset, int len)Reads characters into an array. Returns the number of characters read.

Example 1

The following example shows the usage of Java FileReader class. We’ve created a File reference with name Hello1.txt to be created in current directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.

Open Compiler

packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("Hello1.txt");// creates the file
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char[] a =newchar[50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}

Output

This
is
an
example

Example 2

The following example shows the usage of Java FileReader class. We’ve created a File reference with name Hello1.txt to be created in a provided directory. Then we’re creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we’re reading that file and its contents are printed.

packagecom.tutorialspoint;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassFileDemo{publicstaticvoidmain(String args[])throwsIOException{File file =newFile("F:/Test2/Hello1.txt");// creates the file
      file.createNewFile();// creates a FileWriter ObjectFileWriter writer =newFileWriter(file);// Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();// Creates a FileReader ObjectFileReader fr =newFileReader(file);char[] a =newchar[50];
      fr.read(a);// reads the content to the arrayfor(char c : a)System.out.print(c);// prints the characters one by one
      fr.close();}}

Output

This
is
an
example

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *