Java Path File Name nio checkFile(final String friendlyname, final Path file, final boolean isdirectory, final boolean canwrite)

Here you can find the source of checkFile(final String friendlyname, final Path file, final boolean isdirectory, final boolean canwrite)

Description

Check file access

License

Open Source License

Parameter

Parameter Description
friendlyname friendly name of file for exception messages
file file
isdirectory true if and only if file is expected to be a directory
canwrite true if and only if file is expected to be writable

Exception

Parameter Description
IllegalArgumentException if file == null || !file.exists() || !file.canRead() || (isdirectory && !file.isDirectory()) || (!isdirectory && file.isDirectory()) || (canwrite && !file.canWrite())

Declaration

public static void checkFile(final String friendlyname,
        final Path file, final boolean isdirectory,
        final boolean canwrite) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**/*from   w  ww .  j  a v a  2  s. com*/
 * Copyright (C) 2012-2016 Thales Services SAS.
 *
 * This file is part of AuthZForce CE.
 *
 * AuthZForce CE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * AuthZForce CE 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AuthZForce CE.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    private static final IllegalArgumentException NULL_FILE_ARGUMENT_EXCEPTION = new IllegalArgumentException(
            "Null file arg");

    /**
     * Check file access
     * 
     * @param friendlyname
     *            friendly name of file for exception messages
     * @param file
     *            file
     * @param isdirectory
     *            true if and only if file is expected to be a directory
     * @param canwrite
     *            true if and only if file is expected to be writable
     * @throws IllegalArgumentException
     *             if {@code file == null || !file.exists() || !file.canRead() || (isdirectory && !file.isDirectory()) || (!isdirectory && file.isDirectory()) || (canwrite && !file.canWrite())}
     */
    public static void checkFile(final String friendlyname,
            final Path file, final boolean isdirectory,
            final boolean canwrite) throws IllegalArgumentException {
        if (file == null) {
            throw NULL_FILE_ARGUMENT_EXCEPTION;
        }

        final String exStartMsg = friendlyname + " = '"
                + file.toAbsolutePath() + "' ";
        if (!Files.exists(file)) {
            throw new IllegalArgumentException(exStartMsg + "not found");
        }
        if (!Files.isReadable(file)) {
            throw new IllegalArgumentException(exStartMsg
                    + "cannot be read");
        }
        if (isdirectory && !Files.isDirectory(file)) {
            throw new IllegalArgumentException(exStartMsg
                    + "is not a directory");
        }
        if (!isdirectory && !Files.isDirectory(file)) {
            throw new IllegalArgumentException(exStartMsg
                    + "is not a normal file");
        }
        if (canwrite && !Files.isWritable(file)) {
            throw new IllegalArgumentException(exStartMsg
                    + "cannot be written to");
        }
    }
}

Related

  1. addPath(JarOutputStream outputStream, Path path, String entryName)
  2. addRestorePermissions(String username, Path file)
  3. appendTargetToBuildFile(String targetName, Path dir)
  4. compileAndLoad(Path basePath, String className)
  5. concatFileName(String fileName, Path pathLocation)
  6. convertFilePathToName(Path file)
  7. detectFilePath(String propertyName, String confFileName)