Java Directory Check checkDirectory(File file)

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

Description

Checks that the provided directory path refers to an existing/readable directory.

License

Open Source License

Parameter

Parameter Description
directoryPath the input directory path. Must not be null.

Exception

Parameter Description
IllegalArgumentException in case the specified path doesn't rely on a existing/readable directory.

Return

the re-formatted directory path.

Declaration

public static File checkDirectory(File file) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*//from   w w w  . j ava  2  s. co  m
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 * 
 *    (C) 2003-2008, Open Source Geospatial Foundation (OSGeo)
 *    
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Checks that the provided directory path refers to an existing/readable directory. Finally,
     * return it as a normalized directory path (removing double and single dot path steps if any)
     * followed by the separator char if missing ({@code '/'} On UNIX systems; {@code '\\} on
     * Microsoft Windows systems.
     * 
     * @param directoryPath
     *            the input directory path. Must not be null.
     * @return the re-formatted directory path.
     * @throws IllegalArgumentException
     *             in case the specified path doesn't rely on a existing/readable directory.
     */
    public static File checkDirectory(File file) throws IllegalArgumentException {
        String directoryPath = file.getPath();
        File inDir = file;
        if (!inDir.isDirectory()) {
            throw new IllegalArgumentException("Not a directory: " + directoryPath);
        }
        if (!inDir.canRead()) {
            throw new IllegalArgumentException("Not a writable directory: " + directoryPath);
        }
        try {
            directoryPath = inDir.getCanonicalPath();
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
        /*
         * directoryPath = FilenameUtils.normalize(directoryPath); if
         * (!directoryPath.endsWith(File.separator)){ directoryPath = directoryPath +
         * File.separator; }
         */
        // test to see if things are still good
        inDir = new File(directoryPath);
        if (!inDir.isDirectory()) {
            throw new IllegalArgumentException("Not a directory: " + directoryPath);
        }
        if (!inDir.canRead()) {
            throw new IllegalArgumentException("Not a writable directory: " + directoryPath);
        }
        return new File(directoryPath);
    }
}

Related

  1. checkDirectory(File dir)
  2. checkDirectory(File directory)
  3. checkDirectory(File directory)
  4. checkDirectory(File directory)
  5. checkDirectory(File directory, String pckgname, ArrayList> classes)
  6. checkDirectory(File target)
  7. checkDirectory(final File directory)
  8. checkDirectory(final String dir, final boolean create, final String errorDirName)
  9. checkDirectory(String value)