使用Java创建给定大小的文件

时间:2021-06-18 02:48:26

Is there an efficient way to create a file with a given size in Java?

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

In C it can be done with ftruncate (see that answer).

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

Most people would just write n dummy bytes into the file, but there must be a faster way. I'm thinking of ftruncate and also of Sparse files

大多数人只会在文件中写入n个虚拟字节,但必须有更快的方法。我正在考虑ftruncate和稀疏文件......

2 个解决方案

#1


80  

Create a new RandomAccessFile and call the setLength method, specifying the desired file length. The underlying JRE implementation should use the most efficient method available in your environment.

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

The following program

以下程序

import java.io.*;

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

on a Linux machine will allocate the space using the ftruncate(2)

在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

while on a Solaris machine it will use the the F_FREESP64 function of the fcntl(2) system call.

在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

In both cases this will result in the creation of a sparse file.

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

#2


4  

You can open the file for writing, seek to offset (n-1), and write a single byte. The OS will automatically extend the file to the desired number of bytes.

您可以打开文件进行写入,寻找偏移量(n-1),然后写入单个字节。操作系统会自动将文件扩展到所需的字节数。

#1


80  

Create a new RandomAccessFile and call the setLength method, specifying the desired file length. The underlying JRE implementation should use the most efficient method available in your environment.

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

The following program

以下程序

import java.io.*;

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

on a Linux machine will allocate the space using the ftruncate(2)

在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

while on a Solaris machine it will use the the F_FREESP64 function of the fcntl(2) system call.

在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

In both cases this will result in the creation of a sparse file.

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

#2


4  

You can open the file for writing, seek to offset (n-1), and write a single byte. The OS will automatically extend the file to the desired number of bytes.

您可以打开文件进行写入,寻找偏移量(n-1),然后写入单个字节。操作系统会自动将文件扩展到所需的字节数。