It is rather compact to just create a Scanner, and then call the nextLine on it. But is it as efficient as using BufferedReader? (and what's the case in 1.6?)
Asked
Active
Viewed 635 times
0
-
5I think the word you're looking for is "efficient", not "effective". – Lie Ryan Aug 07 '12 at 15:04
-
pihentagy you don't use `BufferedInputStream` to read text... One difference between `Scanner` and `BufferedReader` is whether the newline character(s) are included. `Scanner.nextLine()` includes the newline character(s), while `BufferedReader.readLine()` does not. – Michael Aug 07 '12 at 15:46
-
@Michael: you are wrong. In `nextLine`'s javadoc: `This method returns the rest of the current line, excluding any line separator at the end.` – pihentagy Aug 08 '12 at 09:13
-
@pihentagy Oops, sorry I misread it. – Michael Aug 08 '12 at 13:52
1 Answers
1
BufferedReader
is more efficient if all you want to do is read each line, as that is all it does.
Scanner
also parses the line, and has a smaller buffer (though this is rarely an issue), so is, at least theoretically, less efficient but a lot easier to use than setting up your own parsing if all you want is simple stuff (so nextDouble()
, nextInt()
etc. are easier with a Scanner
, but line-by-line reading is probably better done with a BufferedReader
)
Also, google is your friend Scanner vs. BufferedReader
-
1Nice SO question you found. Important points from the answers: (1) `Scanner` can do everything `BufferedReader` can, and then some. (2) `Scanner` has a slightly smaller buffer than `BufferedReader` (1KB vs 8KB). (3) `BufferedReader` is synchronized, and `Scanner` is not. – Michael Aug 08 '12 at 14:18