FileInputStream vs FileReader in Java

arpan_banerjee7

Arpan Banerjee

Posted on June 11, 2020

FileInputStream vs FileReader in Java

To understand this thoroughly you need to understand what is character stream and what is byte stream, so let's have a quick look at that.

Byte Streams

A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself. Byte oriented streams do not use any encoding scheme while Character oriented streams use character encoding scheme(UNICODE). All byte stream classes are descended from InputStream and OutputStream .

Character Stream

A character stream will read a file character by character. Character Stream is a higher level concept than Byte Stream . A Character Stream is, effectively, a Byte Stream that has been wrapped with logic that allows it to output characters from a specific encoding . That means, a character stream needs to be given the file's encoding in order to work properly. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc. All character stream classes are descended from Reader and Writer.

If you try to read from a .txt file which has been written with Uni-8 encoding which is by default used in java,then reading the file with both Reader and InputStream classes will give the same output.As here each byte represent one character.

I have created few methods which will help you understand the difference between these two terms--
FileInputStream reads byte by byte
FileReader reads char by char.

Let's dive deeper into these concepts with examples.

Now that you have got an idea about these two streams lets look at the examples to understand how it works internally.

Method to write a some data in a file using Unicode 16 encoding

Alt Text

The output.txt file will contain Hello World in it.

These are the 3 ways of reading from the file first using FileReader the default way, then using FileInputStream and then using InputStreamReader with Unicode-16 charset(encoding).

The comments in the methods are self explanatory please read them to get a clear understanding how it works.

FileReader

Alt Text

The output will be Output of FileInputStream reading byte by byte : þ

FileInputStream

Alt Text

The output will be Output of FileInputStream reading byte by byte : þ

InputStreamReader

Alt Text

The output will be uni-16 ISR: H

So using InputStreamReader we can read the file.

Hope this clears up some doubt on how Java IO works.

Here is the link to the stackoverflow discussion , you can find my answer there as well.

Thank you or reading :)

If you have any question regarding this or anything I should add, correct or remove, feel free to comment, email or DM me. Thanks once again !!

💖 💪 🙅 🚩
arpan_banerjee7
Arpan Banerjee

Posted on June 11, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related