英文:
Read large data using Input Stream in Java
问题
我有一个InputStream对象,需要读取超过50MB的数据。我尝试使用以下代码:
FileOutputStream fos = new FileOutputStream("test.txt");
InputStream fis = response.getResponseStream();
byte[] chunk = new byte[10240000];
int i = 0;
while ((i = fis.read(chunk)) != -1) {
fos.write(chunk, 0, i);
}
但是这需要很长时间。在调试时发现,尽管我使用了10240000字节,但每次只读取了10kb的数据。
我在这里做错了什么吗?请告诉我如何提高性能,一次读取可能达到1MB的数据。
英文:
I have InputStream object which I need to read the data which is very large >50MB. I am trying to use below code:
FileOutputStream fos = new FileOutputStream("test.txt");
InputStream fis = response.getResponseStream();
byte[] chunk = new byte[10240000];
int i = 0;
while ((i = fis.read(chunk)) != -1) {
fos.write(chunk, 0, i);
}
But this is taking a lot of time. On debugging found that this is reading only 10kb at time although I am using 10240000 bytes.
Am I doing something wrong here? Please let me know how to improve the performance and to read maybe 1MB of data at a time.
专注分享java语言的经验与见解,让所有开发者获益!
评论