这是我的建议(假设我们只处理输入流而不考虑如何写入输出流):
- 如果您的用例不需要高读取性能,请使用FileInputStream。例如:
最初的回答
FileInputStream fileInputStream = new FileInputStream("filePath");
byte[] bytes = new byte[1024];
int size;
while ((size = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, size);
}
- 为了获得更好的读取性能,使用BufferedInputStream并坚持使用其默认缓冲区大小,一次只读取一个字节。例如:
最初的回答中提到,为了获得更好的读取性能,可以使用BufferedInputStream,并坚持使用其默认缓冲区大小,一次只读取一个字节。
byte[] bytes = new byte[1];
BufferedInputStream bufferedInputStream =
new BufferedInputStream(fileInputStream("filePath"))
int size;
while ((size = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, size);
}
- 为了获得更好的性能,尝试调整BufferedInputStream的缓冲区大小并逐个字节读取。例如:
为了提高性能,可以尝试通过调整BufferedInputStream的缓冲区大小并逐个字节读取来实现。示例代码如下:
byte[] bytes = new byte[1];
BufferedInputStream bufferedInputStream =
new BufferedInputStream(fileInputStream("filePath"), 16048)
int size;
while ((size = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, size);
}
如果你需要更多的缓存,可以在BufferedInputStream之上使用buffer。例如:
byte[] bytes = new byte[1024];
BufferedInputStream bufferedInputStream =
new BufferedInputStream(fileInputStream("filePath"), 16048)
int size;
while ((size = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, size);
}