java预分配 磁盘,Java用数据填充预分配的文件(在驱动器上)

i want to download a videofile from the web. It is 121 MB. Now i want to pre allocate this 121 MB on the disk (with zeros or what ever) and fill it step by step with the data of the inputstream from the urlconnection. Or with other words: It doesn't matter how much has been downloaded yet - the file size is always the destinated 121MB.

Is it possible?

thank you

解决方案

i got the solution. First of all i write the empty dummy file and then i reopen the empty file and replace the bytes:

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值