Java Directory Check checkDirectory(final String dir, final boolean create, final String errorDirName)

Here you can find the source of checkDirectory(final String dir, final boolean create, final String errorDirName)

Description

Check that a directory is valid.

License

Open Source License

Parameter

Parameter Description
dir the directory name to check
create true if directory should be created when not found
errorDirName name to refer to if an error occurs

Declaration

public static void checkDirectory(final String dir, final boolean create, final String errorDirName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 LegSem./*  w  w  w. j  a v a2s.c  om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     LegSem - initial API and implementation
 ******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Check that a directory is valid.
     * 
     * @param dir the directory name to check
     * @param create true if directory should be created when not found
     * @param errorDirName name to refer to if an error occurs
     */
    public static void checkDirectory(final String dir, final boolean create, final String errorDirName) {
        try {
            checkDirectory(dir, create);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(errorDirName + ": " + e.getMessage());
        }
    }

    /**
     * Check that a directory is valid.
     * 
     * @param fdir the directory name to check
     * @param create true if directory should be created when not found
     * @param errorDirName name to refer to if an error occurs
     */
    public static void checkDirectory(final File fdir, final boolean create, final String errorDirName) {
        try {
            checkDirectory(fdir, create);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(errorDirName + ": " + e.getMessage());
        }
    }

    /**
     * Check that a directory is valid.
     * 
     * @param dir the directory name to check
     * @param create true if directory should be created when not found
     */
    public static void checkDirectory(final String dir, final boolean create) {

        if (dir == null || dir.length() == 0) {
            throw (new IllegalArgumentException("No directory name was specified"));
        }

        checkDirectory(new File(dir), create);
    }

    /**
     * Check that a directory is valid.
     * 
     * @param fdir the directory to check
     * @param create true if directory should be created when not found
     */
    public static void checkDirectory(final File fdir, final boolean create) {

        if (fdir == null) {
            throw (new IllegalArgumentException("No directory name was specified"));
        }

        if (!fdir.exists()) {
            if (!create) {
                throw (new IllegalArgumentException(fdir.getName() + " does not exist"));
            } else {
                if (!fdir.mkdirs()) {
                    throw (new IllegalArgumentException("Could not create directory " + fdir.getName()));
                } else {
                    return;
                }
            }
        }
        if (!fdir.isDirectory()) {
            throw (new IllegalArgumentException(fdir.getName() + " is not a directory"));
        }
        if (!fdir.canWrite()) {
            throw (new IllegalArgumentException("Directory " + fdir.getName() + " is not writable"));
        }
    }
}

Related

  1. checkDirectory(File directory)
  2. checkDirectory(File directory, String pckgname, ArrayList> classes)
  3. checkDirectory(File file)
  4. checkDirectory(File target)
  5. checkDirectory(final File directory)
  6. checkDirectory(String value)
  7. checkDirectoryArray(String[] names)
  8. checkDirectoryInWorkspace(String directoryName)
  9. checkDirPath(final String path)