Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用BufferedOutputStream创建大文件需要很长时间_Java_File_Bufferedinputstream - Fatal编程技术网

Java 使用BufferedOutputStream创建大文件需要很长时间

Java 使用BufferedOutputStream创建大文件需要很长时间,java,file,bufferedinputstream,Java,File,Bufferedinputstream,我有一个文件,它由一个序列化字符串对象组成,该对象写入文件的开头,后跟我试图提取的文件的原始字节 这是我的密码: FileInputStream fileInputStream = new FileInputStream("C:\Test.tst"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); String string = (String) objectInputStream.re

我有一个文件,它由一个序列化字符串对象组成,该对象写入文件的开头,后跟我试图提取的文件的原始字节

这是我的密码:

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();
对于大文件(例如1.5GB),代码需要很长时间才能使用。如何加快代码的速度?我是否使用了错误的类


注意。

您可以尝试通过更改缓冲区大小来微调应用程序

在这里,您已经记录了一个具有缓冲区大小的构造函数版本。也许您可以使用一个大的缓冲区(当然,以牺牲内存使用为代价,也要准备增加堆大小)


首先,我想你不需要它:

ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
String string = (String) objectInputStream.readObject();
。。。您的循环应该更像这样:

final byte[] temp = new byte[1000];
while (fileInputStream.available() > 0){
 int i = fileInputStream.read(temp);
 bufferedOutputStream.write(temp, 0, i);
}

定义一个不可用的长时间-您期望什么时间?与通过“mkfile”创建文件相比,它需要很长的时间吗?例如?记住关闭fileInputStream:]假设文件后有另一个对象,第一个对象是表示文件大小的长类型对象。有可能把文件抽出来吗?temp[1000]超出了文件边界,进入了第二个对象。@dannylancher我不明白您在说什么,但变量I代表您要写入输出流的字节数。也许可以玩这个游戏,或者更清楚地解释你想要实现的目标;)