[英]Creating large file with BufferedOutputStream takes a long time
I have a file which is comprised of one serialized String object written to the start of the file followed by the raw bytes of the file I am attempting to extract. 我有一个文件,该文件由一个序列化的String对象组成,该对象写入文件的开头,然后是我尝试提取的文件的原始字节。
Here is my code: 这是我的代码:
FileInputStream fileInputStream = new FileInputStream("C:\Test.tst");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
String string = (String) objectInputStream.readObject();
FileOutputStream fileOutputStream = new FileOutputStream("C:\ExtractedTest.tst");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
while(fileInputStream.available())
{
int i = fileInputStream.read();
bufferedOutputStream.write(i);
}
bufferedOutputStream.close();
fileOutputStream.close();
The code takes an unusable long time for large files (1.5 GB, for example). 对于大型文件(例如1.5 GB),该代码花费了无法使用的长时间。 How can I speed up the code?
如何加快代码速度? Am I using the wrong classes?
我使用了错误的课程吗?
Regards. 问候。
First of all you don't need that I guess: 首先,您不需要我猜:
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
String string = (String) objectInputStream.readObject();
... and your loop should look more like that: ...,您的循环应该看起来像这样:
final byte[] temp = new byte[1000];
while (fileInputStream.available() > 0){
int i = fileInputStream.read(temp);
bufferedOutputStream.write(temp, 0, i);
}
You can try to finetune your application by changing the buffer size. 您可以尝试通过更改缓冲区大小来微调您的应用程序。
http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html
Here you have documented a version of the constructor with a buffer size. 在这里,您记录了带有缓冲区大小的构造函数的版本。 Maybe you can use a big buffer (at expense of your memory usage, of course, be ready to increase your heap size too)
也许您可以使用大缓冲区(当然,要以牺牲内存使用为代价,也准备增加堆大小)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.