简体   繁体   English

声明扫描仪读取system.in

[英]Declaring scanner to read system.in

Looking at Java tutorials, it seems you have to wrap up multiple layers of objects when declaring a scanner eg http://docs.oracle.com/javase/tutorial/essential/io/scanning.html 查看Java教程,似乎在声明扫描程序时必须包装多层对象,例如http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

has both BufferedReader and FileReader . 同时具有BufferedReaderFileReader However, if I'm reading from System.in do I need to / is there any benefit to doing this? 但是,如果我从System.in读取信息,是否需要这样做? Do the two options behave differently? 这两个选项的行为是否不同?

Scanner s = new Scanner(new BufferedReader(new InputStreamReader(
            System.in)));

vs VS

Scanner s = new Scanner(System.in);

The buffering part is definitely different. 缓冲部分肯定是不同的。 Please read more about IO buffering here: http://docs.oracle.com/javase/tutorial/essential/io/buffers.html 请在此处阅读有关IO缓冲的更多信息: http : //docs.oracle.com/javase/tutorial/essential/io/buffers.html

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. 如果使用得当, BufferedReader阻止从文件中读取的字节转换为字符,然后返回。 So using BufferedReader is recommended. 因此,建议使用BufferedReader

Additionally, you can specify buffer size, that is very handy. 此外,您可以指定缓冲区大小,这非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM