Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Difference Between FileInputStream and FileReader in Java
Next article icon

Difference Between FileInputStream and FileReader in Java

Last Updated : 16 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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/outputReader 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

Next Article
Difference Between FileInputStream and FileReader in Java

S

sanketnagare
Improve
Article Tags :
  • Java
  • Difference Between
  • java-file-handling
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences