Looking at Java tutorials, it seems you have to wrap up multiple layers of objects when declaring a scanner e.g. http://docs.oracle.com/javase/tutorial/essential/io/scanning.html
s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
has both BufferedReader
and FileReader
. However, if I'm reading from System.in
do I need to / is there any benefit to doing this? Do the two options behave differently?
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(
System.in)));
vs
Scanner s = new Scanner(System.in);
Difference is in the efficiency. If used properly BufferedReader
prevents bytes that are read from file to be converted into characters and then returned back. So using BufferedReader
is recommended.
Additionally, you can specify buffer size, that is very handy.