Difference Between FileInputStream and FileReader in Java

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 classimport java.io.FileReader; // Main class// FileReaderExamplepublic 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 packageimport java.io.FileInputStream; // Main classpublic 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
surindertarika1234
sweetyty
java-file-handling
Picked
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Compile-time and Run-time Polymorphism in Java
Similarities and Difference between Java and C++
Difference between Internal and External fragmentation
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java