Java mkdir makeDir(File parentDir, String dirName)

Here you can find the source of makeDir(File parentDir, String dirName)

Description

This can only create a directory in an existing parent directory (meaning, it won't create any ancestor directories if they don't exist).

License

Open Source License

Declaration

public static File makeDir(File parentDir, String dirName) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright 2012, 2013 Unconventional Thinking
 *
 * This file is part of Hierarchy.//from   ww w.j a  v  a2  s .  c  o  m
 *
 * Hierarchy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * Hierarchy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Hierarchy.  
 * If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * This can only create a directory in an existing parent directory (meaning, it won't create any ancestor directories if they don't exist).
     */
    public static File makeDir(String dirPath) throws IOException {
        return makeDir(dirPath, false);
    }

    /**
     * This method can create any non-existing ancestor directories.
     */
    public static File makeDir(String dirPath, boolean createAncestorDirectories) throws IOException {
        File dir = new File(dirPath);
        return makeDir(dir, createAncestorDirectories);
    }

    /**
     * This can only create a directory in an existing parent directory (meaning, it won't create any ancestor directories if they don't exist).
     */
    public static File makeDir(File parentDir, String dirName) throws IOException {
        return makeDir(parentDir, dirName, false);
    }

    /**
     * This method can create any non-existing ancestor directories.
     */
    public static File makeDir(File parentDir, String dirName, boolean createAncestorDirectories)
            throws IOException {
        File dir = new File(parentDir, dirName);
        return makeDir(dir, createAncestorDirectories);
    }

    /**
     * This can only create a directory in an existing parent directory (meaning, it won't create any ancestor directories if they don't exist).
     */
    public static File makeDir(File dir) throws IOException {
        return makeDir(dir, false);
    }

    /**
     * This method can create any non-existing ancestor directories.
     */
    public static File makeDir(File dir, boolean createAncestorDirectories) throws IOException {
        File parentDir = dir.getParentFile();

        if (createAncestorDirectories) {
            dir.mkdirs();
        } else {
            if (!parentDir.exists()) {
                throw new IOException(
                        "The parent directory of director to be created does not exist. Parent directory path is: "
                                + parentDir.getPath());
            }
            if (!parentDir.canWrite()) {
                throw new IOException("The parent directory can not be modified: " + parentDir.getPath());
            }

            dir.mkdir();
        }

        return dir;
    }
}

Related

  1. makeDir(File dir)
  2. makeDir(File dir)
  3. makeDir(File f)
  4. makeDir(File file)
  5. makeDir(File file)
  6. makeDir(File parentFile)
  7. makedir(String containerDir, String name)
  8. makeDir(String dir)
  9. makeDir(String dir)