简体   繁体   English

Java Inputstream太慢,无法读取1.5mb文件

[英]Java Inputstream too slow to read 1.5mb file

I am working on a part of a project. 我正在做项目的一部分。 Its quite big for a newbie like me. 对于像我这样的新手来说,这相当大。 In one section I have to fetch an attachment from email and save it to system file. 在一个部分中,我必须从电子邮件中获取附件并将​​其保存到系统文件中。 But for some reason, its taking too long time. 但是由于某种原因,它花费的时间太长。 I used this code for 1st approach: 我将此代码用于第一种方法:

byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) != -1) {
    fileOutputStream.write(buf, 0, bytesRead);
}

This code block is taking about 86 secs just for a 1.5Mb file. 仅对于1.5Mb文件,此代码块大约需要86秒。 But when I try to run this same code from a sample test project, It finished in several milliseconds (For this I took the inputstream of a system file instead of attachment and then write it in another directory ). 但是,当我尝试从一个示例测试项目中运行相同的代码时,它在几毫秒内完成了(为此,我获取了系统文件的输入流而不是附件,然后将其写入另一个目录中)。 I already visited this pages: Java: InputStream too slow to read huge files 我已经访问过以下页面: Java:InputStream太慢,无法读取大文件

Input stream reads large files very slowly, why? 输入流读取大文件的速度非常慢,为什么?

But I cant find out the specific reason of this. 但我无法找出造成这种情况的具体原因。 I tried to debug it more by this approach: 我尝试通过这种方法对其进行更多调试:

InputStream inputStream = bodyPart.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
long tStart = System.currentTimeMillis();
byte[] buf = new byte[4096];
int bytesRead;
int i=1;
while(true)
{
    long tc = System.currentTimeMillis();
    bytesRead = inputStream.read(buf);
    long tc1 = System.currentTimeMillis();
    System.out.println("\nRead Time "+i+" ="+(tc1-tc));

    if(bytesRead==-1) break;
    fileOutputStream.write(buf, 0, bytesRead);

    long tc2 = System.currentTimeMillis();
    System.out.println("Write Time "+i+" ="+(tc2-tc1));
    i++;
}

long tEnd = System.currentTimeMillis();
long tDelta = tEnd - tStart;
System.out.println("\n\nTotal Time="+tDelta+"\n\n");

And the output is also weird. 而且输出也很奇怪。 A part of output for a 1.5Mb file is: 1.5Mb文件的部分输出为:

Read Time 354 =788
Write Time 354 =0

Read Time 355 =0
Write Time 355 =0

Read Time 356 =0
Write Time 356 =0

Read Time 357 =744
Write Time 357 =0

Read Time 358 =0
Write Time 358 =0

Read Time 359 =0
Write Time 359 =0

Read Time 360 =837
Write Time 360 =0

Read Time 361 =0
Write Time 361 =0

Read Time 362 =1
Write Time 362 =0

Read Time 363 =811
Write Time 363 =0

Read Time 364 =1
Write Time 364 =0

Read Time 365 =0
Write Time 365 =0

Read Time 366 =757
Write Time 366 =0

Read Time 367 =1
Write Time 367 =0

Read Time 368 =0
Write Time 368 =0

Read Time 369 =736
Write Time 369 =0

Read Time 370 =0
Write Time 370 =0

Read Time 371 =0
Write Time 371 =0

Read Time 372 =484
Write Time 372 =0

Read Time 373 =0

Total Time=88796

Here, as you can see the read time for 4Kb buffer is taking quite long time after each 3 step or read. 在这里,您可以看到,每进行3步或读取一次,4Kb缓冲区的读取时间就会花费很长时间。 I cant figure out the specific issue. 我不知道具体的问题。 I have also used buffered input Stream and output Stream, but the results are same.Can anybody help me to find out what is the actual problem? 我也使用了缓冲输入流和输出流,但是结果是相同的,有人可以帮我找出实际问题是什么吗?

使用bufferedinputstream而不是InputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM