在 Java 中创建具有给定大小的文件

2022-08-31 17:48:13

有没有一种有效的方法可以在Java中创建具有给定大小的文件?

在C中,可以用ftruncate完成(参见答案)。

大多数人只会将n个虚拟字节写入文件,但必须有更快的方法。我想到了ftruncateSparse文件...


答案 1

创建一个新的 RandomAccessFile 并调用 setLength 方法,指定所需的文件长度。基础 JRE 实现应使用环境中可用的最有效方法。

以下程序

import java.io.*;

class Test {
     public static void main(String args[]) throws Exception {
           RandomAccessFile f = new RandomAccessFile("t", "rw");
           f.setLength(1024 * 1024 * 1024);
     }
}

在 Linux 机器上将使用 ftruncate(2) 分配空间

6070  open("t", O_RDWR|O_CREAT, 0666)   = 4
6070  fstat(4, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
6070  lseek(4, 0, SEEK_CUR)             = 0
6070  ftruncate(4, 1073741824)          = 0

而在 Solaris 机器上,它将使用 fcntl(2) 系统调用的 F_FREESP64 函数。

/2:     open64("t", O_RDWR|O_CREAT, 0666)               = 14
/2:     fstat64(14, 0xFE4FF810)                         = 0
/2:     llseek(14, 0, SEEK_CUR)                         = 0
/2:     fcntl(14, F_FREESP64, 0xFE4FF998)               = 0

在这两种情况下,这都将导致创建稀疏文件。


答案 2

从Java 8开始,此方法适用于Linux和Windows:

final ByteBuffer buf = ByteBuffer.allocate(4).putInt(2);
buf.rewind();

final OpenOption[] options = { StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW , StandardOpenOption.SPARSE };
final Path hugeFile = Paths.get("hugefile.txt");

try (final SeekableByteChannel channel = Files.newByteChannel(hugeFile, options);) {
    channel.position(HUGE_FILE_SIZE);
    channel.write(buf);
}