8

From what I can read on Stack Overflow, the FileReader only takes files, where the FileInputStream can read all. But is there any advantages in using the FileReader then? Is it faster?

I read that:

"FileReader does not allow you to specify an encoding and instead uses the plaform default encoding, which makes it pretty much useless as using it will result in corrupted data when the code is run on systems with different platform default encodings."

But didn't quite get it. What does platform default encodings mean?

informatik01
  • 16,038
  • 10
  • 74
  • 104
user3161344
  • 83
  • 1
  • 7
  • possible duplicate: http://stackoverflow.com/questions/5155226/fileinputstream-vs-filereader?rq=1 – AnV Aug 19 '16 at 07:03

6 Answers6

14

The Main difference is:
Stream - Byte Based(Read or write Bytes)
Reader - Character Based(Read or Write Character)

Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
13
  • An InputStream is a stream from which you can read bytes. You wouldn't normally use this class directly, but if you did, you'd be expecting binary data. It has many different subclasses for input from different sources, such as FileInputStream, AudioInputStream, StringBufferInputStream and so on.

  • An InputStreamReader is a wrapper for an InputStream, that converts the stream's bytes into characters, using any encoding you like. Normally, you specify the character encoding when you create the InputStreamReader. There are a few different constructors that let you do this. If you want to read text data, you could use an InputStreamReader. Make your InputStream first to read the data, then wrap it in an InputStreamReader.

  • A FileReader is a specialised InputStreamReader that can only read from files, and uses the default platform encoding. In other words, it assumes that the file that it reads has been created according to your operating system's settings for platform encoding. This is usually OK; but if you're going to be reading files with different encodings, you should create a FileInputStream and wrap it in an InputStreamReader.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • But lets say I have a txt file. Is there any advantages in using the FileReader? – user3161344 Jan 04 '14 at 22:27
  • @user3161344 Yes, that is what a FileReader is for. FileInputStream is for reading binary. – Peter Lawrey Jan 04 '14 at 22:29
  • Yes. It saves you the hassle of converting bytes to characters yourself, which is not a trivial problem. – Dawood ibn Kareem Jan 04 '14 at 22:32
  • But in a program I made I used the FileInputStream to do that. So im trying to find out what the advantages might be. – user3161344 Jan 04 '14 at 22:33
  • No. A `FileInputStream` doesn't convert bytes to characters. If you want to get a `String` out of a `FileInputStream`, you need to convert them using some other mechanism. – Dawood ibn Kareem Jan 04 '14 at 22:34
  • FileInputStream fstream = new FileInputStream(filnavn); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); Was how I did it – user3161344 Jan 04 '14 at 22:40
  • I see an `InputStreamReader` lurking in that snippet. – Dawood ibn Kareem Jan 04 '14 at 22:40
  • Ah yeah. But I might aswell have used a Filereader. Is it any quicker with a FileReader since it only handles chars? – user3161344 Jan 04 '14 at 22:46
  • 2
    A `FileReader` is an `InputStreamReader`. It's a subclass. So it's using the same code - it's not going to outperform `InputStreamReader`. What it doesn't do is buffering - you've put in your own `BufferedReader`, so you've already dealt with that, and that may help you with performance. Also, get rid of the `DataInputStream` - you're not actually using its functionality at all. – Dawood ibn Kareem Jan 04 '14 at 22:48
2

Platform default encoding, means the encoding used by the OS, that the JVM is running on.

Andres
  • 10,561
  • 4
  • 45
  • 63
2

If you are in Windows go to Control Panel -> Regional and Language Options Control Panel -> Advanced there you will see default encoding. FileReader always uses that encoding.

What makes FileReader different from FileInputStream is that FileReader is for reading text files in default encoding while FileInputStream is for reading binary files.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

A FileReader is a character stream while a FileInputStream is a byte stream. In the first case default encoding is relevant (consider for example InputStreamReader as intermedium bridge).

If you want another encoding for a character stream (that is a Reader) then use:

FileInputStream in = ...;
String charSet = ...; // for example UTF-8
Reader reader = new InputStreamReader(in, charSet);
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

FileInputStream and FileOutputStream are used for object serialization / deserialization. Also just use it if you need to work with bytes.

In other cases, you need characters, so you should use FileReader/FileWriter. For example:

BufferedReader readFile = new BufferedReader(new FileReader(file));
solvator
  • 371
  • 1
  • 6
  • 12