简体   繁体   English

Java.io.InputStream.read()方法将每个数据类型读取为字节吗?

[英]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 . 如果需要读取字符,请使用ReaderInputStream读取它们。

(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 - 这种抽象的目的是分离责任-

  • The InputStream provides a stream of bytes and handles all underlying logic (file reading / network / ...). InputStream提供字节流并处理所有基础逻辑(文件读取/网络/ ...)。
  • The 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.

相关问题 java.io.InputStream.read()如何工作? - How does java.io.InputStream.read() work? java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int java.io.InputStream.read(byte[])” - java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference KSoap2 throws Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference for some reason - KSoap2 throws Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference for some reason java.io.InputStream `read()` 方法与 `read(byte[] b)` 相比为什么输出不同的值? - Why java.io.InputStream `read()` method outputs different value if compared with `read(byte[] b)`? Java InputStream 的 read(byte[]) 方法 - Java InputStream's read(byte[]) method 如何通过java中InputStream中的read函数读取一个字节并将其作为int返回? - How is one byte read and returned as an int by read function in InputStream in java? Java InputStream.read(byte [],int,int)方法,如何阻塞,直到读取了确切的字节数 - Java InputStream.read(byte[], int, int) method, how to block until the exact number of bytes has been read InputStream read()方法永远不会返回-1(Java) - InputStream read() method never returns -1 (Java) Java 是否将读取的 position 保存在 InputStream 中? - Does Java save the read position in the InputStream? InputStream.read(byte)方法如何工作? - How could InputStream.read(byte) method ever work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM