Create File with Given Size in Java

Create file with given size in Java

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[]) 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)

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.

Generate a file of a specific size using Java's File

Try this code.

        File file = new File("/home/ok.txt");
RandomAccessFile rafile;
try {
rafile = new RandomAccessFile(file, "rw");
rafile.setLength(1024);

} catch (Exception e) {
e.printStackTrace();
}

Create a file of a given size and type in java

Got it finally. The magic number for a jar file is (50 4b 03 04) Taken from here

File file = new File(basedir, filePath);
file.getParentFile().mkdirs();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(sizeInBytes);
byte[] magicHeader = new byte[4];
magicHeader[0] = 0x50;
magicHeader[1] = 0x4b;
magicHeader[2] = 0x03;
magicHeader[3] = 0x04;
raf.write(magicHeader, 0, 4);
raf.close();

Generate file of given size

You just create the file and fill it with the size you want. You don't 'set the size'. If you create a file and put 1 kilobytes of stuff into it the file will be 1 kilobyte (*slightly larger actually).

So just create a file, and use whatever file write routine to output bytes which you can create with (byte)(random()*510 - 255) in a loop for however many bytes large you want the file to be. Since this is java and you may want to use print to output to the file, you have to use (char)(Math.random()*510 - 255) otherwise it will print the string "65" instead of "A"

Creating a file of the desired memory size

The Java char type is 16-bits long and OutputStreamWriter objects are string-oriented, not byte-oriented. Depending on the output encoding of the writer, you could get e.g. a 2 KB file, or even something unpredictable on character sets with a variable number of bytes per character.

You would be better off using an OutputStream directly, either with a byte[] or using write(int) in a loop. That would guarantee that you get what you are asking for.

Most efficient way to create a large file ( 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



Related Topics



Leave a reply



Submit