-1

I'm really confused. I've seen a lot of implementations on how to read a file in Java, and the more I read, the less it makes sense to me.

As I noticed, the preferred way to read a file is to use BufferedReader over Scanner. But, I've seen examples like:

Scanner s = new Scanner(new BufferedReader(...);

Instead of just:

Scanner s = new Scanner(new File("foo.txt"));

What would be the reason to initialize a Scanner with BufferedReader? Are there any advantages to do it this way?

Now, what I find more confusing is, everywhere I looked, there are suggestions for using Path Object instead of File. But, I found no elegant way of initializing BufferedReader with Path Objects, as both BufferedReader & FileReader cannot accept Path.

There is of course a way to just do something like:

Path path = Paths.get("foo.txt");
BufferedReader br = new BufferedReader(new FileReader(path.toFile()));

But that looks like a lot of mess.

My questions regarding BufferedReader are:

  • Should I even use BufferedReader?
  • What's the purpose of FileReader? Why Can't I just initialize BufferedReader with a String or Path?
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
  • Yes you should use BufferedReader. The answer to the other question is - `just because you can doesn't mean you should` which applies to the first one as well. – Shark Jul 13 '16 at 08:54
  • @Shark I don't understand - you said I should, but then suggested I shouldn't, which one is it? – Alex Weitz Jul 13 '16 at 08:59
  • 1
    You should. As for the "Why Can't I just initialize BufferedReader with a String or Path" - there are alot of workarounds and shortcuts in programming that seem to make things easier as they cut out the needless, possibly complex parts out. But just because you can doesn't mean you should. As for your last question - why cant it be initialized with a string, that'd be a good question for the author of the class. In the end, read up some more, and make an informed decision whether you should in that particular case or not. – Shark Jul 13 '16 at 09:06
  • 1
    This: http://stackoverflow.com/a/9648877/3501205 Helped me understand the FileReader & BufferedReader concept. – Alex Weitz Jul 13 '16 at 09:29

1 Answers1

3

Should I even use BufferedReader?

Sure, why not?

What's the purpose of FileReader?

To read files in character mode.

Why Can't I just initialize BufferedReader with a String or Path?

Because it doesn't have such a constructor, and if it did it would be a FileReader, which already exists, so it would be redundant.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks, and now I read that `FileReader` shouldn't be used at all, and instead `InputStreamReader` should be used. I wish there was one clear way of doing stuff, I asked the question so I could really understand the proper way of handling this task. – Alex Weitz Jul 13 '16 at 09:17
  • 1
    There isn't one 'proper way'. There are several ways, depending on your requirement. You might for example want to construct your own `InputStreamReader` with a non-default charset; or a `BufferedReader` with a non-default buffer size; or your input may not be coming from a file at all. – user207421 Jul 13 '16 at 09:54
  • @AlexWeitz i'm glad you figured it out. Nows a good time to propose a `FileInputStream` and consider whether it fits with the requirement :) – Shark Jul 13 '16 at 12:19