使用Java中的输入流读取大数据

huangapple 未分类评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月27日 22:00:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/63116947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定