Java Create Directory mkdirs(File path)

Here you can find the source of mkdirs(File path)

Description

mkdirs

License

Apache License

Parameter

Parameter Description
path a parameter

Exception

Parameter Description
IOException if path not exists and could not be created.

Declaration

public static File mkdirs(File path) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/*from  w w  w .j a  v a2 s.  c o m*/
     * @param path
     * @throws IOException if path not exists and could not be created.
     */
    public static File mkdirs(File path) throws IOException {
        path = getCleanAbsolutePath(path);
        // todo find parents and throw exception if the first existing is not a directory
        if (!path.exists()) {
            if (!path.mkdirs()) {
                throw new IOException("Could not mkdirs " + path.getAbsolutePath());
            }
            //      log.info("Created directory " + path.getAbsolutePath());
        }
        return path;
    }

    /**
     * Makes "/tmp/a/b/c/./../.." into "/tmp/a/b/c"
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static File getCleanAbsolutePath(File path) throws IOException {
        path = new File(path.getAbsolutePath());
        while (path.isDirectory()) {
            if (".".equals(path.getName())) {
                path = path.getParentFile();
            } else if ("..".equals(path.getName())) {
                path = path.getParentFile();
            } else {
                break;
            }
        }
        return path;
    }
}

Related

  1. mkdirs(File outdir, String path)
  2. mkdirs(File parent, String name, boolean mkdirs)
  3. mkdirs(File path)
  4. mkdirs(File path)
  5. mkDirs(File path)
  6. mkdirs(final File aDirectory)
  7. mkdirs(final File dirs)
  8. mkdirs(final File f)
  9. mkdirs(final File file)