Code example for RandomAccessFile

Methods: close, setLength

0
     */ 
    public static File generateFile(int size) {
        File file = new File("./test" + "//" + getFileName(size));
 
        try { 
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            raf.setLength(1024 * size);
            raf.close();
 
        } catch (IOException e) {
            e.printStackTrace();
        } 
 
        return file;
    } 
 
    private static String getFileName(int size) {
        String unit = "KB";
 
        if (size < 1024 * 1024)
            unit = "MB";