Difference Between FileInputStream and FileReader in Java
Last Updated : 16 Dec, 2021
Let us first do discuss them in order to get the understanding alongside an example to interpret the differences. Here first we will be discussing out FileReader class. So starting of with FileReader class in java is used to read data from the file. It returns data in byte format like FileInputStream class. It is a character-oriented class that is used for file handling in java. This class inherits from the InputStreamReader class. FileReader is used for reading streams of characters.
Syntax:
Creating using the name of the file
FileReader input = new FileReader(String name);
Creating using an object of the file
FileReader input = new FileReader(File fileObj);
Implementation:
Let us consider a sample file where we are reading the data from the text file Gfg.txt using the Java FileReader class. We are assuming that we have the following data in file namely the “Gfg.txt” file as follows:
Welcome to GeeksForGeeks.
Example
Java // Java Program to Illustrate FileReader class // Importing class import java.io.FileReader; // Main class // FileReaderExample public class GFG { // Main driver method public static void main(String args[]) throws Exception { // Creating an object of FileReader class to // read content from file in local directory FileReader fr = new FileReader("C:\\Gfg.txt"); int i; // Reads from the file while ((i = fr.read()) != -1) { // Printing the content inside the file System.out.print((char)i); } // Closing the connections to // avoid memory space fr.close(); } }
Output:
Welcome to GeeksForGeeks
Now dwelling on the second way that is via FileInputStream class. It is present in the same package namely java.io which retrieves bytes from a file. It's used to read byte-oriented data (raw bytes streams) like image data, audio, and video. You can also read data from a character stream. However, the FileReader class is recommended for reading streams of characters.
Syntax:
Creating using the path to file
FileInputStream input = new FileInputStream(stringPath);
Creating using an object of the file
FileInputStream input = new FileInputStream(File fileObject);
Note: Before running the code, a text file named "Gfg.txt" is required to be created. In this file, we are having the following content: “Welcome to GeeksForGeeks”
Example
Java // Java Program to Illustrate FileInputStream class // Importing class from java.io package import java.io.FileInputStream; // Main class public class GFG { // Main driver method public static void main(String args[]) { // Try block to check for exceptions try { // Creating an object of FileInputStream class // in main() body to get file FileInputStream input = new FileInputStream("Gfg.txt"); // Display message only System.out.println("Data in the file: "); // Reading the first byte int i = input.read(); while (i != -1) { System.out.print((char)i); // Reads next byte from the file using // next() method i = input.read(); } // Closing the file input.close(); } // Catch block to handle the exceptions catch (Exception e) { // Print the exception on the console System.out.println(e); } } }
Output:
Data in the file: Welcome to GeeksForGeeks
Now after having an understanding of both of them let us conclude out the differences between them which are depicted below in the tabular format.
FileInputStream | FileReader |
---|
Stream is a byte-based object that can read and write bytes. | Reader is Character Based, it can be used to read or write characters. |
FileInputStream is Byte Based, it can be used to read bytes. | FileReader is Character Based, it can be used to read characters. |
Stream is used to binary input/output | Reader is used to character input/output |
FileInputStream is used for reading binary files. | FileReader is used for reading text files in platform default encoding. |
Serialization and DeSerialization can be done with FileInputStream and ObjectInputStream, and serialized objects can be saved to a file. Serialization converts an object to a byte stream, and deserialization converts it back to an object. | FileReader is not used for Serialization and DeSerialization, as it reads characters not bytes. |
FileInputStream is descendant of InputStream class. | FileReader extends from Reader class |
read() method of FileInputStream can read one byte at a time or multiple bytes in a byte array. | read() method of FileReader can read one character at a time or multiple characters into an array |
Similar Reads
Difference Between FileInputStream and ObjectInputStream in Java FileInputStream class extracts input bytes from a file in a file system. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. It should be used to read byte-oriented data for example to read audio, video, images,
5 min read
Difference Between BufferedReader and FileReader in Java BufferedReader and FileReader both classes are used to read data from a given character stream. Both of them have some pros and some cons. In this article, we will discuss the differences between them. Though the most important difference is in the way they work, but we will discuss the other detail
10 min read
Difference Between ZipFile and ZipInputStream in Java Zip files are basically an archive file that is used to compress all the files in one place. Doing this reduces memory space occupied and makes transport of files bundle easy. ZipInputStream which are used to read the file entries in zip files present in the zip. In Java, there are two classes namel
5 min read
Difference Between ObjectInputStream and ObjectOutputStream in Java In a software project, in many instances, there are necessities to transfer the data, and it can be handled by using ObjectOutputStream and ObjectInputStream from Java IO packages. Usually, the data is written in binary format and hence we cannot view the contents. Serialization is the process of wr
8 min read
InputStreamReader class in Java An InputStreamReader is a bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted. Declaration : public cl
4 min read