create File for number of bytes - Android java.io

Android examples for java.io:File

Description

create File for number of bytes

Demo Code

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main{

    public static void createFile(File file, int numBytes)
            throws IOException {
        File parentFile = file.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();/* www. j  a  v  a 2  s.c  o m*/
        }
        byte[] buffer = new byte[numBytes];
        FileOutputStream output = new FileOutputStream(file);
        try {
            output.write(buffer);
        } finally {
            output.close();
        }
    }

}

Related Tutorials