Java FileReader Class
In Java, the FileReader
class is a convenient way to read the content of files. It is part of the java.io
package and provides methods to read data as characters, making it useful for reading text-based files like .txt
or .csv
. In this guide, we will explore the features of the FileReader
class, how to use it, and provide sample code for practical understanding.
The FileReader
class is a subclass of InputStreamReader
and provides a simple way to read files in Java. It reads characters from files and is best suited for reading text files. Unlike FileInputStream
, which reads byte streams, FileReader
reads data as characters, making it easier to work with character-based data like text, XML, JSON, etc.
.txt
, .csv
, and .json
.The FileReader
class has two common constructors:
FileReader(String fileName) throws FileNotFoundException
This constructor creates a new FileReader
object to read from the file specified by the file name.
FileReader(File file) throws FileNotFoundException
This constructor creates a FileReader
object using the File
object.
To effectively use the FileReader
class in Java, you typically need to combine it with other classes like BufferedReader
for efficient reading of lines from the file.
In this example, we will show you how to read a text file character by character using FileReader
.
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExample {
public static void main(String[] args) {
FileReader reader = null;
try {
// Create FileReader object
reader = new FileReader("example.txt");
// Read the file character by character
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// Close the FileReader
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileReader
object and specify the file we want to read (example.txt
).read()
method reads one character at a time and returns an integer representing the character’s ASCII value.-1
).FileReader
is closed using close()
in the finally
block to ensure proper resource management.To read files more efficiently, we often use BufferedReader
along with FileReader
. BufferedReader
reads large chunks of data into memory, making it much faster than reading one character at a time.
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
FileReader reader = null;
BufferedReader bufferedReader = null;
try {
// Create FileReader and BufferedReader objects
reader = new FileReader("example.txt");
bufferedReader = new BufferedReader(reader);
String line;
// Read the file line by line
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// Close the BufferedReader and FileReader
if (bufferedReader != null) {
bufferedReader.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedReader
is used to read the file line by line using readLine()
.While both FileReader
and FileInputStream
can be used to read files in Java, they have key differences:
Use FileReader
when you are dealing with text data, and FileInputStream
for binary data.
Since FileReader
works with files, it may throw exceptions like FileNotFoundException
or IOException
. Proper exception handling is crucial for graceful error management.
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileReaderWithExceptionHandling {
public static void main(String[] args) {
try {
// Trying to read a non-existing file
FileReader reader = new FileReader("nonexistent.txt");
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error while reading the file: " + e.getMessage());
}
}
}
FileNotFoundException
when the file does not exist.IOException
.FileReader
after use to free up system resources.BufferedReader
along with FileReader
.