2

I read a tutorial 'Read file in String with BufferedInputStream', and I got a code from here:

http://examples.javacodegeeks.com/core-java/io/bufferedinputstream/read-file-in-string-with-bufferedinputstream/

The question is this line: // byte array to store input byte[] contents = new byte[1024];

So, how can I ensure it is 1024 byte? If I have a data with 1025 byte, and my code will break. So, how can I make it more generic? Thanks.

4

2 回答 2

1

在您引用的代码中,数组通过调用填充

byte[] contents = new byte[1024];
int bytesRead=0;
bytesRead = bin.read(contents));

bin.read 将查看内容的大小并从流中读取最多 1024 个字节。

于 2013-08-12T11:13:03.280 回答
0

不,它不会破裂。

魔法就在这里

while ((bytesRead = bin.read(contents)) != -1) {

bin.read(contents))它将bytes从 中读取下一组file

我问的问题:缓冲区字节数组如何在流式传输时不断填充?

于 2013-08-12T11:12:16.307 回答