Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.IOException; import android.text.TextUtils; public class Main { public static boolean createFile(String fileName) throws IOException { if (TextUtils.isEmpty(fileName)) { return false; } return createFile(fileName, false); } public static boolean createFile(String fileName, boolean forceCreate) throws IOException { if (TextUtils.isEmpty(fileName)) { return false; } return createFile(new File(fileName), forceCreate); } public static boolean createFile(File file) throws IOException { if (file == null) { return false; } return createFile(file, false); } public static boolean createFile(File file, boolean forceCreate) throws IOException { if (file == null) { return false; } if (file.exists()) { if (!forceCreate) { return false; } else { file.delete(); } } if (!isExistDirectory(file.getParentFile().getAbsolutePath())) { file.getParentFile().mkdirs(); } return file.createNewFile(); } public static boolean isExistDirectory(String directory) { if (TextUtils.isEmpty(directory)) { return false; } boolean result = false; File file = new File(directory); if (file.exists() && file.isDirectory()) { result = true; } return result; } }