The Java 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.
Following is the declaration for Java.io.FileReader class −
public class FileReader extends InputStreamReaderField
Following are the fields for Java.io.FileReader class −
protected Object lock − This is the object used to synchronize operations on this stream.
FileReader(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.
Example - Using FileReader(String fileName) to Read a FileThe 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.javapackage 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 exampleExample - Using FileReader(String fileName) to Read a File
The following example shows the usage of Java FileReader class.
FileReaderDemo.javapackage 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
Creates a FileReader object using new FileReader(fileName), which opens "example.txt" for reading.
Reads the file character by character using fileReader.read(). The method returns an integer (-1 when the end of the file is reached). Characters are cast from int to char before printing.
Uses try-with-resources to ensure that FileReader is automatically closed after execution.
Handles exceptions properly using catch(IOException e), which prevents crashes if the file is missing.
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