我正在尝试决定哪种方式更适合读取文件,一次读取字节。这两种方法中的一种是否比另一种更好,如果是,为什么?
1)
byte[] buffer = new byte[FILE_RETRIEVAL_BUFFER_SIZE];
int bytesRead = fileContent.read(buffer);
while (bytesRead != 1) {
fileOnDisk.write(buffer, 0, bytesRead);
bytesRead = fileContent.read(buffer);
}
2)
byte[] buffer = new byte[FILE_RETRIEVAL_BUFFER_SIZE];
while (true) {
int bytesRead = fileContent.read(buffer);
if (-1 == bytesRead)
{break;}
fileOnDisk.write(buffer, 0, bytesRead);
}