我想从网络下载视频文件。大小为 121 MB。现在我想在磁盘上预先分配这 121 MB(用零或其他什么)并用来自 urlconnection 的输入流的数据逐步填充它。或者换句话说:无论已经下载了多少内容 - 文件大小始终是目标 121MB。
这可能吗? 谢谢
最佳答案
我找到了解决方案。首先,我编写空的虚拟文件,然后重新打开空文件并替换字节:
System.out.println("Writing dummy ...");
byte buf[] = new byte[1024];
for (int size = 0; size < fileLength; size += buf.length) {
out.write(buf);
out.flush();
}
out.close();
System.out.println("Writing data ...");
RandomAccessFile raf = new RandomAccessFile(temp, "rw");
int count = 0;
long total = 0;
while ((count = stream.read(buf)) > 0) {
raf.seek(total);
raf.write(buf);
total += count;
}
raf.close();
关于Java 用数据填充预分配文件(在驱动器上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16588142/