Java Canonical Path Create getCanonicalDirectory(String path, String errPrefix)

Here you can find the source of getCanonicalDirectory(String path, String errPrefix)

Description

Retrieves the canonicalized directory from the specified path.

License

Open Source License

Parameter

Parameter Description
path the path to test
errPrefix a String to use to provide a specific error message

Exception

Parameter Description
IllegalArgumentException if there is an IOException accessing the directory orif the path does not exist or if the path is not a directory

Return

the canonicalized

Declaration

public static File getCanonicalDirectory(String path, String errPrefix) 

Method Source Code

//package com.java2s;
/**/*ww w  . j a  v a2s  .  c o  m*/
 * Copyright (c) 2009 Stephen Evanchik
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Stephen Evanchik - initial implementation
 */

import java.io.File;
import java.io.IOException;

public class Main {
    /**
     * Retrieves the canonicalized directory from the specified path.
     *
     * @param path
     *            the path to test
     * @param errPrefix
     *            a String to use to provide a specific error message
     * @return the canonicalized {@link File}
     *
     * @throws IllegalArgumentException
     *             if there is an {@link IOException} accessing the directory or
     *             if the path does not exist or if the path is not a directory
     */
    public static File getCanonicalDirectory(String path, String errPrefix) {
        File rc;
        try {
            rc = new File(path).getCanonicalFile();
        } catch (IOException e) {
            throw new IllegalArgumentException(errPrefix + " '" + path + "' : " + e.getMessage()); //$NON-NLS-1$ $NON-NLS-2$
        }
        if (!rc.exists()) {
            throw new IllegalArgumentException(errPrefix + " '" + path + "' : does not exist"); //$NON-NLS-1$ $NON-NLS-2$
        }
        if (!rc.isDirectory()) {
            throw new IllegalArgumentException(errPrefix + " '" + path + "' : is not a directory"); //$NON-NLS-1$ $NON-NLS-2$
        }
        return rc;
    }
}

Related

  1. getCanonicalCharset(String charset)
  2. getCanonicalCharsetQuiet(String charset)
  3. getCanonicalDirectory(final String rawDir)
  4. getCanonicalFile(@Nullable final File aFile)
  5. getCanonicalFile(File f)
  6. getCanonicalFile(File file)
  7. getCanonicalFile(File file)