Java Utililty Methods Directory Create

List of utility methods to do Directory Create

Description

The list of methods to do Directory Create are organized into topic(s).

Method

voidmkdirs(File f)
Makes sure that the directory in which the file is exists.
File p = f.getParentFile();
if (!p.exists())
    p.mkdirs();
booleanmkDirs(File f)
create all missing folders and return true, if folder on success
boolean ok = false;
if (f.isDirectory())
    ok = true;
else if (f.exists())
    ok = false;
else {
    ok = f.mkdirs();
return ok;
voidmkdirs(File f)
mkdirs
if (f.exists()) {
    if (!f.isDirectory())
        throw new RuntimeException("Expected directory " + f.getAbsolutePath() + " but found a file.");
    return;
if (!f.mkdirs())
    throw new RuntimeException("Cannot create directory " + f.getAbsolutePath());
Listmkdirs(File f)
Like File.mkdirs() this creates all parent directories of f, but returns them in a list
ArrayList<File> files = new ArrayList<File>();
if (f.isDirectory()) {
    files.add(f);
File parentDir = f.getParentFile();
while (parentDir != null && !parentDir.exists()) {
    files.add(parentDir);
    parentDir = parentDir.getParentFile();
...
voidmkdirs(File file)
mkdirs
if (file.isDirectory()) {
    return;
if (!file.mkdirs()) {
    if (file.exists()) {
        throw new IOException("failed to create " + file + " file exists and not a directory");
    throw new IOException();
...
booleanmkdirs(File file)
Create all directories, including nonexistent parent directories.
if (file != null) {
    String parent = file.getParent();
    if (parent != null) {
        File parentDir = new File(parent);
        if (!parentDir.exists()) {
            return parentDir.mkdirs();
    return true;
return false;
voidmkdirs(File file)
Check and create missing parent directories for the given file.
if (file.exists() && !file.isFile()) {
    throw new IOException("File " + file.getPath() + " is actually not a file.");
File parentFile = file.getParentFile();
if (!parentFile.exists() && !parentFile.mkdirs()) {
    throw new IOException("Creating directories " + parentFile.getPath() + " failed.");
voidmkdirs(File file)
Create a directory hierarchy, raise an exception in case of failure.
if (file != null) {
    if (!file.mkdirs()) {
        throw new IOException("Failed to create directories");
voidmkdirs(File file)
mkdirs
if (!file.exists()) {
    file.mkdirs();
booleanmkdirs(File file)
A safer version of File.mkdirs, which works around a race in the 1.5 JDK where two VMs creating the same directory chain at the same time could end up in one VM failing to create a subdirectory.
final File parentFile = file.getAbsoluteFile().getParentFile();
if (!parentFile.exists()) {
    mkdirs(parentFile);
if (parentFile.exists()) {
    return file.mkdir();
} else {
    return false;
...