InputStreamReader vs FileReader

FileReader reads character from a file in the file system. InputStreamReader reads characters from any kind of input stream. The stream could be a FileInputStream, but could also be a stream obtained from a socket, an HTTP connection, a database blob, whatever.

I usually prefer using an InputStreamReader wrapping a FileInputStream to read from a file because it allows specifying a specific character encoding.


First, InputStreamReader can handle all input streams, not just files. Other examples are network connections, classpath resources and ZIP files.

Second, FileReader until Java 11 did not allow you to specify an encoding and instead only used the plaform default encoding, which made it pretty much useless as using it would result in corrupted data when the code is run on systems with different platform default encodings.

Since Java 11, FileReader is a useful shortcut for wrapping an InputStreamReader around a FileInputStream.

Tags:

Java

Stream