Java FileReader Example

The FileReader class of java.io package provides a seamless and easy-to-use way to read and analyse the contents of a file using Java. The FileReader class, works similar to the FileInputStream class because it reads the file data as a stream. However, unlike the latter which reads files as a stream of bytes, FileReader reads the input as a stream of characters. The operating procedure of FileReader is easy to comprehend as it reflects how anyone would read a document, or file, i.e. “as texts”.

Let us look into how FileReader works in Java.

Constructors

The FileReader class in Java has three parameterized with which one can access and read a file. They are:

  • FileReader(File file) – This constructor creates an object of FileReader which accepts a java.io.File object as a parameter and opens it in default ‘Read’ mode.
  • FileReader(FileDescriptor descriptor)This constructor creates an object of FileReader which accepts a java.io.FileDescriptor object as a parameter and opens the file referenced by the descriptor object in ‘Read’ mode.
  • FileReader(String filePath) – This constructor accepts a java.lang.String object as a parameter and opens the file referenced by the path or URI contained in the String

If the file is not found or cannot be opened, a FileNotFoundException is thrown.

Methods

The FileReader class contains helper methods that one can use to read the contents of a file. As the FileReader reads the file as a stream of characters, only one character is read at a time till the end of file.

Let us take a look at the methods we use to read files using FileReader.

  • public int read() throws IOException – The default read() method reads a character from the file and returns the integer value (ASCII) for the character read.
    It returns -1 when the end of file is reached.
  • public int read(char [] a,int offset, int length)The overriden read() method reads the characters of the file from the offset into a character array(represented here by the variable ‘a’). It returns the number of characters read.
  • public void close() – The close method closes the FileReader object so that there is no resource leaks.

Example

Let us look at an example of reading a file with FileReader class.

Code:

Output:

Hey this is a sample text read using FileReader class.

In the above example, we read a file containing the text “Hey this is a sample text read using FileReader class” from a file ‘sample.txt’. We use the String parameterized constructor of FileReader class to obtain a FileReader object and then read the characters present in the file till End of File is reached(i.e. Character read is -1).

Related Posts

  • Set an array to another array in java
    23 April

    Set an Array Equal to Another Array in Java

    Table of ContentsSetting an Array Variable Equal to Another Array VariableSet an Array Equal to Another Array in Java Using the clone() MethodSet an Array Equal to Another Array in Java Using the arraycopy() MethodSet an Array Equal to Another Array in Java Using the copyOf() MethodSet an Array Equal to Another Array in Java […]

  • Java random number between 1 and 100
    07 March

    Generate Random Number Between 1 and 100 in Java

    Table of ContentsUsing Java Util’s Random Class to Generate Random Number Between 1 and 100 in JavaThe nextint() MethodThe ints() MethodUsing Math Class to to Generate Random Number Between 1 and 100 in JavaUsing Apache Commons Library to Generate Random Number Between 1 and 100 in JavaUsing ThreadLocalRandom Library to Generate Random Number Between 1 […]

  • java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
    22 February

    [Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

    Table of ContentsReason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListFixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayListUse ArrayList’s constructorAssign Arrays.asList() to List reference rather than ArrayList In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList. ClassCastException is runtime exception which indicate that code has tried to […]

  • HashMap values cannot be cast to list
    21 February

    [Fixed] java.util.HashMap$Values cannot be cast to class java.util.List

    Table of ContentsWhy HashMap values cannot be cast to list?Fix for java.util.HashMap$Values cannot be cast to class java.util.List In this post, we will see how to fix error java.util.HashMap$Values cannot be cast to class java.util.List. Why HashMap values cannot be cast to list? HashMap values returns java.util.Collection and you can not cast Collection to List […]

  • Format number with Commas in Java
    20 February

    Format Number with Commas in Java

    Table of ContentsHow To Add Commas to Number in Java1. Using DecimalFormat2. Using String’s format() method3. Using System.out.printf4. Using Formatter5. Using NumberFormat In this post, we will see how to format number with commas in java. How To Add Commas to Number in Java There are multiple ways to format number with commas in java. […]

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.