A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/io/java_io_filereader.htm below:

Java - FileReader Class

Java - FileReader Class Introduction

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

Class declaration

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

public class FileReader
   extends InputStreamReader
Field

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

Class constructors Sr.No. Constructor & Description 1

FileReader(File file)

This constructor creates a new FileReader, given the File to read from.

2

FileReader(FileDescriptor fd)

This constructor creates a new FileReader, given the FileDescriptor to read from.

3

FileReader(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.

Example - Using FileReader(String fileName) to Read a File

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.

FileReaderDemo.java
package com.tutorialspoint;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderDemo {

   public static void main(String args[])throws IOException {
      File file = new File("Hello1.txt");
      
      // creates the file
      file.createNewFile();
      
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(file); 
      
      // Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();

      // Creates a FileReader Object
      FileReader fr = new FileReader(file); 
      char [] a = new char[50];
      fr.read(a);   // reads the content to the array
      
      for(char c : a)
         System.out.print(c);   // prints the characters one by one
      fr.close();
   }
}
Output
This
is
an
example
Example - Using FileReader(String fileName) to Read a File

The following example shows the usage of Java FileReader class.

FileReaderDemo.java
package com.tutorialspoint;

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

public class FileReaderDemo {
    public static void main(String[] args) {
        String fileName = "example.txt"; // File to be read

        try (FileReader fileReader = new FileReader(fileName)) {
            int character;
            while ((character = fileReader.read()) != -1) {
                System.out.print((char) character); // Print each character
            }
        } catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
Output(if example.txt contains "Hello, World!")
Hello, World!
Explanation Example - Read a File Character by Character

The following example shows the usage of Java FileReader class. We've created a File reference with name Hello1.txt to be created in the 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.

package com.tutorialspoint;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderDemo {

   public static void main(String args[])throws IOException {
      File file = new File("Hello1.txt");
      
      // creates the file
      file.createNewFile();
      
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(file); 
      
      // Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();

      // Creates a FileReader Object
      FileReader fr = new FileReader(file); 
      char [] a = new char[50];
      fr.read(a);   // reads the content to the array
      
      for(char c : a)
         System.out.print(c);   // prints the characters one by one
      fr.close();
   }
}
Output
This
is
an
example

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4