[英]How FileInputStream read work?
Someone please explain me how buffer array is filled in the following code: 有人请向我说明以下代码如何填充缓冲区数组:
try {
byte[] buffer = new byte[1024];
//(1) this print an empty string?
System.out.println("1: " + new String(buffer));
FileInputStream inputStream = new FileInputStream("test.txt");
int len;
while((len = inputStream.read(buffer)) != -1) {
//(2) this print text on my file?
System.out.println("2: " + new String(buffer));
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Update : i have got useful info after reading following: https://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html 更新 :阅读以下内容后,我得到了有用的信息: https : //docs.oracle.com/javase/tutorial/essential/io/bytestreams.html
Buffered input streams read data from a memory area known as a buffer;
缓冲的输入流从称为缓冲区的存储区读取数据; the native input API is called only when the buffer is empty.
仅当缓冲区为空时才调用本机输入API。 Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full
同样,缓冲的输出流将数据写入缓冲区,并且仅当缓冲区已满时才调用本机输出API
Thanks all! 谢谢大家!
The InputStream#read
method will read data from the input stream into the supplied buffer. InputStream#read
方法将从输入流中读取数据到提供的缓冲区中。
See the java doc for details. 有关详细信息,请参见Java文档 。
(In particular, see the return value – it will return -1 if there is no more data to read.) (特别是,请参见返回值–如果没有更多数据可读取,它将返回-1。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.