Difference between Stream (FileInputStream) and Reader (FileReader) in java file handling

In this post we will be discussing differences between FileInputStream and FileReader

Difference between FileInputStream and FileReader in java file IO >

Stream - FileInputStream
Reader - FileReader
Stream is Byte Based, it can be used to read bytes or write bytes.
Reader is Character Based, it can be used to read or write characters.
Stream is used to binary input/output
Reader is used to character input/output
FileInputStream is Byte Based, it can be used to read bytes.
FileReader is Character Based, it can be used to read characters.
FileInputStream is used for reading binary files.
FileReader is used for reading text files in platform default encoding.

Now question comes comes, What is Platform default encoding?
Platform default encoding means the encoding used by the operating system on which  the JVM is running.

Example - Platform default encoding is user specific setting.
Windows (in US) - it's often CP1250,
Windows (in Europe) - it's often CP1252.
Windows (in China) - it's often Big5.

On many other windows and  Linux systems - it's often UTF-8.

On Macs - it’s often MacRoman.
FileInputStream and ObjectInputStream can be used for Serialization and DeSerialization, where serialized object can be persisted in file. In Serialization object is converted into byte stream and in deserialization it is converted back from byte to object.
FileReader is not used for Serialization and DeSerialization, as it reads characters not bytes.
reads 1 byte (8-bit) at a time.
FileReader.read() reads 2 bytes(16-bit) at a time, because char is 16-bit data type.
FileInputStream must be used when we are reading audio, video or other multimedia files.
FileReader must be used when we are reading text files, pdfs or word documents.

8. FileReader does not allow you to specify character encoding and will always use the platform default character encoding , and will result in corrupted data when the code is run on some other platform which have different platform default character encoding and hence limits platform independence in java.

Rather than using
FileReader new FileReader("c:/myFile.txt");

we must use
new InputStreamReader( new FileInputStream("c:/myFile.txt"), "UTF-8");
An InputStreamReader is a wrapper for an InputStream, that converts the stream's bytes into characters and allows to define any encoding.

For further understanding, you must take a look at below two snippets to figure out how we can above two code snippets with BufferedReader -

How to use FileReader with BufferedReader >
FileReader fr = new FileReader("c:/myFile.txt");
BufferedReader br = new BufferedReader(fr);

How to use InputStreamReader with BufferedReader >
InputStreamReader isr=new InputStreamReader(new FileInputStream("c:/myFile.txt"), "UTF-8");
BufferedReader br = new BufferedReader(isr);


Difference between FileInputStream and BufferedInputStream in java file IO

Difference between FileReader and BufferedReader in java file IO

Read text from file using FileInputStream in java file IO

Program to Read text from file using FileInputStream and Try with resource provided in java 7

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
//start paginationJs //end paginationJs