Difference Between FileInputStream and FileReader in Java


When working with file input in Java, engineers regularly come over two commonly utilized classes: FileInputStream and FileReader. Both classes serve the purpose of reading information from records, but they differ in their approaches and utilization scenarios. In this article, we'll look at the contrasts between FileInputStream and FileReader, their dialect structure, and their particular code cases.

Syntax

FileInputStream

FileInputStream inputStream = new FileInputStream("file.txt");

FileReader

FileReader fileReader = new FileReader("file.txt");

Explanation of Syntax

FileInputStream

The FileInputStream class is utilized to read binary information from records. It takes a record way as a parameter and makes a stream for reading information from the specified record.

FileReader

The FileReader lesson, on the other hand, is utilized to read character information from records. It moreover takes a record way as a parameter and makes a reader for reading character information from the specified record.

Approach 1: FileInputStream

The FileInputStream class is basically utilized for reading binary information, such as pictures, sound records, or any other non-textual information. Here is the algorithm for utilizing FileInputStream −

Algorithm

  • Create an instance of FileInputStream by providing the file path.

  • Create a buffer or an array of bytes to hold the data read from the file.

  • Use the read() method of FileInputStream to read data from the file into the buffer/array.

  • Process the data as needed.

  • Close the FileInputStream using the close() method to release system resources.

Example

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample {
   public static void main(String[] args) {
      try {
         FileInputStream inputStream = new FileInputStream("file.txt");
         byte[] buffer = new byte[1024];
         int bytesRead = inputStream.read(buffer);
         while (bytesRead != -1) {
            String data = new String(buffer, 0, bytesRead);
            System.out.print(data);
            bytesRead = inputStream.read(buffer);
         }
         inputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Hello, World!
This is a sample text file.
It contains some text for demonstration purposes.
Feel free to modify or replace this content as needed.

In this case, we make an occasion of FileInputStream with the record way "file.txt". We at that point make a byte cluster called "buffer" to store the information perused from the record. The read() strategy is utilized to examined information into the buffer, and we handle the information as required. The read() strategy returns the number of bytes studied, and on the off chance that it returns -1, it shows the conclusion of the record. Finally, we close the FileInputStream to release system resources.

Approach 2: FileReader

The FileReader class is specifically designed for reading character-based data from files, such as text files. Here is the algorithm for using FileReader −

Algorithm

  • Create an instance of FileReader by providing the file path.

  • Create a buffer or an array of characters to hold the data read from the file.

  • Use the read() method of FileReader to read data from the file into the buffer/array.

  • Process the data as needed.

  • Close the FileReader using the close() method to release system resources.

Example

import java.io.FileReader;
import java.io.IOException;

public class FileReaderExample {
   public static void main(String[] args) {
      try {
         FileReader fileReader = new FileReader("file.txt");
         char[] buffer = new char[1024];
         int charsRead = fileReader.read(buffer);
         while (charsRead != -1) {
            String data = new String(buffer, 0, charsRead);
            System.out.print(data);
            charsRead = fileReader.read(buffer);
         }
         fileReader.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Hello, World!
This is a sample text file.
It contains some text for demonstration purposes.
Feel free to modify or replace this content as needed.

In this illustration, we make an occurrence of FileReader with the record way "file.txt". We at that point make a char array called "buffer" to store the information examined from the record. The examined() strategy is utilized to examine information into the buffer, and we prepare the information as required. The examined() strategy returns the number of characters studied, and in case it returns -1, it shows the conclusion of the record. Finally, we close the FileReader to release system resources.

Difference Between FileInputStream and FileReader in Java

Property

FileInputStream

FileReader

Purpose

Used for reading binary data

Used for reading character data

Data

Reads data as bytes

Reads data as characters

Encoding

No built-in encoding support

Supports encoding conversions

Usage

Suitable for non-textual data

Suitable for textual data

Performance

Faster for reading binary data

Slower for reading large files or non-textual data

Conclusion

In outline, FileInputStream and FileReader are both valuable classes in Java for reading information from records, but they have diverse purposes and are utilized in totally different scenarios. FileInputStream is reasonable for reading binary information, whereas FileReader is perfect for reading character-based information, such as content records. By understanding their differences and appropriate use cases, developers can make informed decisions when working with file input in Java.

Updated on: 28-Jul-2023

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements