[英]does Java.io.InputStream.read() Method read every datatype as byte?
if the read method reads a byte of data from the input stream, when it has to read a char, does it read twice byte by byte? 如果read方法从输入流中读取一个字节的数据,那么当它必须读取char时,是否逐字节读取了两次? as a char is of 2 byes?
作为一个字符是2再见?
InputStream
operates on bytes. InputStream
对字节进行操作。 It is the underlying I/O abstraction in Java. 它是Java中的基础I / O抽象。 It can read a single byte or a sequence of bytes, depending on what the caller requests.
它可以读取单个字节或字节序列,具体取决于调用者的请求。 But it knows nothing about characters, so it cannot, by itself, decide to read two bytes for a character.
但是它对字符一无所知,因此它本身不能决定读取一个字符的两个字节。 A
Reader
would have to request this. Reader
将不得不要求这样做。
If you need to read characters, use Reader
to read them from the InputStream
. 如果需要读取字符,请使用
Reader
从InputStream
读取它们。
(Similarly, to read serialized Java objects, you would use ObjectInputStream
, which again reads them from the InputStream
. Or you can use Scanner
to read a variety of inputs from numbers to text, again from an InputStream
.) (类似地,要读取序列化的Java对象,您可以使用
ObjectInputStream
,该对象再次从InputStream
读取它们。或者您可以使用Scanner
从数字到文本再从InputStream
读取各种输入。)
The purpose of this abstraction is separation of responsibilities - 这种抽象的目的是分离责任-
InputStream
provides a stream of bytes and handles all underlying logic (file reading / network / ...). InputStream
提供字节流并处理所有基础逻辑(文件读取/网络/ ...)。 Reader
converts the stream of bytes to stream of characters, and doesn't care where the data came from. Reader
将字节流转换为字符流,并且不在乎数据来自何处。 根据在线提供的Oracle文档https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(),它读取字节数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.