开发者

Most efficient way to create a large file (> 1GB)

I would like to know what is the most efficient way to creat开发者_Python百科e a very large dummy File in java. The filesize should be just above 1GB. It will be used to unit test a method which only accepts files <= 1GB.


Create a sparse file. That is, open a file, seek to a position above 1GB and write some bytes.

Relevant: Create file with given size in Java


Can't you make a mock which returns filesize of > 1GB? File IO doesn't sound very unit-testy to me (although that depends on what your idea of a unit test is).


Made this function to create sparse files

private boolean createSparseFile(String filePath, Long fileSize) {
    boolean success = true;
    String command = "dd if=/dev/zero of=%s bs=1 count=1 seek=%s";
    String formmatedCommand = String.format(command, filePath, fileSize);
    String s;
    Process p;
    try {
        p = Runtime.getRuntime().exec(formmatedCommand);

        p.waitFor();
        p.destroy();
    } catch (IOException | InterruptedException e) {
        fail(e.getLocalizedMessage());
    }
    return success;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜