Saturday, 22 April 2017

Difference between FileReader vs FileInputStream in Java


Difference between FileReader vs FileInputStream in Java


Even though both FileReader and FileInputStream are used to read data from a file in Java, they are quite a different. The main difference between the FileReader and FileInputStream is that one read data from character stream while other read data from a byte stream. The FileReader automatically converts the raw bytes into character by using platform's default character encoding. This means you should use this class if you are reading from a text file which has same character encoding as the default one. If you happen to read a text file encoded in different character encoding then you should use InputStreamReader with specified character encoding. An InputStreamReader is a bridge between byte stream and character stream and can take a FileInputStream as a source. Though, it's worth remembering that it caches the character encoding which means you cannot change the encoding scheme programmatically.



As per my recruitment experience, I have often found Java developers good on multi-threading, collections and basics but fare really poor on Java IO. They struggle to answer questions like channel vs buffer, byte vs character or 
IO vs NIO. If you ask them to write code, they never handle the exception, never close the file and hardly write production quality code. An interview is a place to write your best code.




Brushing up your IO skills by reading 
Core Java for Really Impatient is also a good idea, as one reason of lacking Java IO knowledge is the lack of practice. This book, not only explains things well but also give you lots of exercises to practice well.

Difference between FileReader vs FileInputStream in Java




Differences between FileReader and FileInputStream
Here are a couple of key differences between a FileReader and a FileInputStream in Java:

1) The first difference is in their type hierarchy, 
FileReader extends from Reader class while FileInputStream is descendent of InputStream class.

2) The second difference is in their purpose. The FileReader is meant for reading text data while 
FileInputStream is for reading binary data.

3) The various overloaded read() method of FileReader can read one character at a time or multiple characters into an array while read() method of FileInputStream can read one byte at a time or multiple bytes in a 
byte array.

Here is a nice slide which explains the difference between FileReader and FileInputStream quite well:

Difference between FileReader vs FileInputStream in Java



Java Program to Read File using FileReader vs FileInpustStream
Let's see an example to learn how to use both FileReader and FileInputStream classes to read data from text file. Remember, FileReader reads character while FileInputStream reads bytes. In this program, I have just read the data from the manifest.mf file which is a text file.



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Java Program to demonstrate difference between
 * FileReader and FileInputStream in Java
 *
 * @author WINDOWS 8
 */
public class IteratorRemoveTest {

    public static void main(String args[]) throws IOException {
       
        // reading data using FileReader
        System.out.println("Reding text file using FileReader");
        try {
            FileReader fr = new FileReader("manifest.mf");
            int i = fr.read();
           
            while(i != -1){
                System.out.print((char)i);
                i = fr.read();
            }
          
        } catch (FileNotFoundException ex) {
            Logger.getLogger(IteratorRemoveTest.class.getName()).log(Level.SEVERE, null, ex);
        }
       
       
        // reading data using FileInpustStream
        System.out.println("Reding text file using FileInputStream");
        try {
           
            FileInputStream fis = new FileInputStream("manifest.mf");
            int b = fis.read();
           
            while(b != -1){
                System.out.print(b);
                b = fis.read();
            }
           
        } catch (FileNotFoundException ex) {
            Logger.getLogger(IteratorRemoveTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

Output:
Reding text file using FileReader
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Reding text file using FileInputStream
779711010510210111511645861011141151051111105832494648131088456779777769788458327797105110456710897115115321191051081083298101329710010010110032971171161111099711610599971081081213298121329811710510810013101310BUILD SUCCESSFUL (total time: 0 seconds)


That's all on the difference between FileReader and FileInputStream in Java. So, use FileReader if you intend to read streams of character and use the FileInputSream if you want to read streams of raw bytes. It's better to use FileReader for 
reading text files because it will take care of converting bytes to characters but remember that FileReader uses the platform's default character encoding. If you are reading a file which is encoded in a character encoding other than the host's default char encoding then you should use the InputStreamReader.

No comments:

Post a Comment