Difference Between FileInputStream and FileReader in Java
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 |
Please Login to comment...