Java FileReader Class

The FileReader class in Java is used to read character data from a file. It extends the InputStreamReader class and provides a convenient way to read text files in Java.

Here’s an example of how to use the FileReader class:

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

public class FileReaderExample {
   public static void main(String[] args) {
      try {
         FileReader reader = new FileReader("example.txt");
         int character;

         while ((character = reader.read()) != -1) {
            System.out.print((char) character);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

In this example, we create a FileReader object and pass the name of the file we want to read as a parameter to its constructor. We then use a while loop to read characters from the file until the end of the file is reached (indicated by the read() method returning -1). Finally, we use System.out.print() to print each character to the console.

It’s important to note that FileReader reads characters, not bytes. If you need to read binary data from a file, you should use the FileInputStream class instead. Also, when you’re done using a FileReader object, you should always close it using the close() method to release any resources it may be holding.

Java FileReader class declaration:

The FileReader class is declared in the java.io package in Java. Its declaration is as follows:

public class FileReader extends InputStreamReader

This means that the FileReader class extends the InputStreamReader class, which is used to read bytes and decode them into characters using a specified character set. By extending the InputStreamReader class, the FileReader class inherits all of its methods and adds a few of its own for reading characters from a file.

The FileReader class provides constructors for creating objects that can read from a file in various ways. For example, you can create a FileReader object by passing a file name, a File object, or a FileDescriptor object to the constructor. You can also specify the character encoding to use for reading the file, or use the default encoding if you don’t specify one.

Once you have a FileReader object, you can use its read() method to read characters from the file. The read() method returns an int value that represents the next character in the file, or -1 if the end of the file has been reached. You can also use other methods provided by the FileReader class, such as skip() and mark(), to manipulate the reading position in the file.

Constructors of FileReader class:

The FileReader class provides several constructors for creating FileReader objects. These constructors are:

  1. FileReader(String fileName) – Creates a new FileReader object that reads from the specified file. The file name should be a string that represents the path to the file.
  2. FileReader(File file) – Creates a new FileReader object that reads from the specified File object.
  3. FileReader(FileDescriptor fd) – Creates a new FileReader object that reads from the specified file descriptor.

Each of these constructors throws an IOException if there is a problem opening the file.

Here’s an example of how to use the FileReader constructors:

import java.io.*;

public class FileReaderExample {
   public static void main(String[] args) {
      try {
         // Using a file name
         FileReader reader1 = new FileReader("example.txt");

         // Using a File object
         File file = new File("example.txt");
         FileReader reader2 = new FileReader(file);

         // Using a FileDescriptor object
         FileInputStream inputStream = new FileInputStream(file);
         FileDescriptor fd = inputStream.getFD();
         FileReader reader3 = new FileReader(fd);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

In this example, we create three FileReader objects using different constructors. The first one uses a file name, the second one uses a File object, and the third one uses a FileDescriptor object obtained from a FileInputStream object.

Methods of FileReader class:

The FileReader class provides several methods for reading characters from a file. These methods include:

  1. read() – Reads a single character from the file and returns it as an integer value. Returns -1 if the end of the file has been reached.
  2. read(char[] cbuf) – Reads characters into an array and returns the number of characters read. Returns -1 if the end of the file has been reached.
  3. read(char[] cbuf, int offset, int length) – Reads characters into a portion of an array and returns the number of characters read. Returns -1 if the end of the file has been reached.
  4. skip(long n) – Skips over n characters in the file and returns the number of characters actually skipped.
  5. ready() – Returns true if the file is ready to be read, false otherwise.
  6. close() – Closes the FileReader object and releases any resources it may be holding.

Here’s an example of how to use some of these methods:

import java.io.*;

public class FileReaderExample {
   public static void main(String[] args) {
      try {
         FileReader reader = new FileReader("example.txt");
         char[] buffer = new char[1024];
         int numCharsRead;

         // Read characters into a buffer
         numCharsRead = reader.read(buffer);

         // Print out the characters that were read
         for (int i = 0; i < numCharsRead; i++) {
            System.out.print(buffer[i]);
         }

         // Skip over the first 10 characters in the file
         reader.skip(10);

         // Read a single character from the file
         int c = reader.read();

         // Check if the file is ready to be read
         boolean ready = reader.ready();

         // Close the file
         reader.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

In this example, we create a FileReader object and use the read() method to read characters into a buffer. We then print out the characters that were read, skip over the first 10 characters in the file using the skip() method, read a single character from the file using the read() method again, and check if the file is ready to be read using the ready() method. Finally, we close the file using the close() method.

Java FileReader Example:

Here’s an example of how to use the FileReader class in Java to read characters from a file:

import java.io.*;

public class FileReaderExample {
   public static void main(String[] args) {
      try {
         // Create a FileReader object
         FileReader reader = new FileReader("example.txt");

         // Read characters from the file
         int c;
         while ((c = reader.read()) != -1) {
            System.out.print((char) c);
         }

         // Close the file
         reader.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

In this example, we create a FileReader object and use a while loop to read characters from the file one at a time. The read() method returns the next character in the file as an integer value, which we cast to a char and print to the console. We continue reading characters from the file until the end of the file is reached, which is indicated by a return value of -1 from the read() method. Finally, we close the file using the close() method to release any resources it may be holding.

Note that the example assumes that there is a file named “example.txt” in the current working directory. If the file is located elsewhere, you will need to provide the full path to the file when creating the FileReader object.