Java mkdir makeDirsFor(File file)

Here you can find the source of makeDirsFor(File file)

Description

Make the directories for the given file to exist in, if they don't already exist.

License

Open Source License

Parameter

Parameter Description
file non-null; the file to make directories for

Declaration

static public void makeDirsFor(File file) 

Method Source Code

//package com.java2s;
// the rights to use, copy, modify, merge, publish, distribute, sublicense,

import java.io.File;

public class Main {
    /**//from w ww .j  a v a  2s  . c o m
     * Make the directories for the given file to exist in, if they don't
     * already exist. That is, make directories for all but the last
     * component of the given file. Also make sure the final directory is
     * writable, throwing an exception if that is not the case.
     *
     * @param file non-null; the file to make directories for 
     */
    static public void makeDirsFor(File file) {
        if (file == null) {
            throw new NullPointerException("file == null");
        }

        File parent = file.getParentFile();

        if (!parent.exists()) {
            if (!parent.mkdirs()) {
                throw new RuntimeException("Unable to make directory: " + parent);
            }
        }

        if (!parent.isDirectory()) {
            throw new RuntimeException("Not a directory: " + parent);

        }

        if (!parent.canWrite()) {
            throw new RuntimeException("Unable to write to directory: " + parent);
        }
    }
}

Related

  1. makedirs(String fileName)
  2. makeDirs(String fileName, boolean hasFile)
  3. makeDirs(String filePath)
  4. makeDirs(String strBottomFoldName, String strFoldName)
  5. makeDIRS(String[] dirs, File path)
  6. makeDirsWhenNotExist(String filePath)