Java的ClassLoader机制
一些有趣的C语言问题

关于java中System.in与System.out的思考

tcsong posted @ Thu, 03 Sep 2009 00:11:15 -1100 in Programming Language with tags java , 2404 readers

最近在工作上涉及到system的一些源代码,突然想到一个问题:System.out是流,却拥有各种各样的print方法来打印字符,这样大大方便了程序的编写;那么,为什么system.in没有这样的方法呢?

就这个问题我跟同事们探讨了一下,也到usenet上去问了,感谢大家的帮助,最终总结出比较靠谱的解释如下:

System.in与System.out都是字节流而非字符流,PrintStream所做的工作,就是当输出一个非字节流类型的数据的时候,通过平台相关的转换算法将其转化为字节流,但其本质仍然是一个个的字符

而在读取的时候,如果采用相同的处理方法,则会产生这样的一个问题:如何判断一个数据的终止?

就拿int为例,如果读入的int是字符形式的,那如何得知数据长度?没有数据长度就无法停止读取。也许有人提出可以采用分隔符的方式来终止读取,这样做的弊端是作为分隔符的那个字符就被舍弃了,所以sun提供了java.util.scanner来实现此功能而不是将其作为默认配置。如果int是纯字节流呢?那很遗憾,这样就与输出不匹配了,输入输出的一致性始终是最先要考虑的因素。

总的来说,在输出时,java能够清楚地知道用户想输出的是什么,并进行统一的处理;但是在输入时,java难道能够知道用户读的是什么吗?不知道。所以就没有提供一个统一的处理方法,而是交给用户根据自己的需要对输入流进行封装

Avatar_small
NCERT Computer Quest said:
Wed, 28 Sep 2022 18:30:25 -1100

In modern days, Computer Education is most important for everyone and its one of the fastest growing career fields. A career in the field of computer science has been proven to be a worthwhile direction for any young enthusiast. It helps them to aim for excellent jobs in the future and succeed in it. NCERT Computer Question Paper Class 9 The computer has become a standard of education throughout the world. This makes computer education important.In modern days, Computer Education is most important for everyone and its one of the fastest growing career fields.

Avatar_small
Emma said:
Wed, 02 Nov 2022 01:38:53 -1100

In java programming, System.in is the input stream connected to the console, such as System. out is the output stream connected to the console. So nice to see the cbd benefits details you have mentioned over here and I am looking here to more updates regarding that. Keep sharing more updates here.

Avatar_small
rov said:
Wed, 07 Dec 2022 02:03:44 -1100

There are a few things to consider when using System.in and System.out in Java. First, System.in is a buffered input stream, meaning that it can buffer input from the keyboard or other input devices. System.out, on the other hand, is a buffered output stream, which can buffer output from the system console or other output devices. Secondly, System.in and System.out are byte streams, meaning that they deal with bytes, rather than characters. This can be an issue when trying to read or write non-ASCII characters, as they may be read or written incorrectly. Finally, System.in and System.out are not thread-safe, usage of CBD so care must be taken when using them in

Avatar_small
charlly said:
Fri, 16 Dec 2022 20:56:28 -1100

There are a couple different ways to think about System.in and System.out in Java. On one hand, they can be seen as convenience methods that make it easier to read from and write to the console. On the other hand, they can be seen as CBD Supplements limiting because they only work with the console and not with files or other streams.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter