tags:

views:

1890

answers:

2

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

In C it can be done like in that answer.

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

+3  A: 

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.

Greg Hewgill
Will the resulting file be sparse?
sk
sk: In general, yes. In practice, this depends on the specifics of the underlying OS and might require some fiddling to make the file sparse.
Greg Hewgill
On Unix and Linux, that will be a sparse file - one block of zero bytes apart from the single byte written (which might also be zero, of course).
Jonathan Leffler
+20  A: 

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.

The following program

import java.io.*;

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

on a Linux machine will allocate the space using the 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.

/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.

Diomidis Spinellis
How did you record these library call traces? ^^
Adrian
They are system call traces. I used strace(1) under Linux and truss(1) under Solaris.
Diomidis Spinellis
In javadoc, it is written that "In this case, the contents of the extended portion of the file are not defined".Does this still guaranties to be zeroed on windows and linux, or is there any efficient way to be sure all bytes are zeros?
Sarmun
I would expect that any OS worth its salt (this includes Windows and Linux) to zero the bytes, to avoid the leakage of older data belonging to another user. However, I can think of scenarios where this would not be the case: old data belonging to the same process, or a small (J2ME) platform.
Diomidis Spinellis