Java用数据填充预分配的文件(在驱动器上) [英] Java Fill preallocated File with Data (on drive)

查看:150
本文介绍了Java用数据填充预分配的文件(在驱动器上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网络上下载视频文件。它是121 MB。现在,我想在磁盘上预分配这121 MB的空间(用零或其他值),并逐步使用urlconnection的inputstream数据填充它。或换句话说:下载多少无关紧要-文件大小始终是预定的121MB。

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

这篇关于Java用数据填充预分配的文件(在驱动器上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆