Java Path Exist nio checkFileExists(Path path, LinkOption... options)

Here you can find the source of checkFileExists(Path path, LinkOption... options)

Description

<P>Checks if a file exists - <B>Note:</B> according to the <A HREF="http://docs.oracle.com/javase/tutorial/essential/io/check.html">Java tutorial - Checking a File or Directory</A>: </P> <PRE> The methods in the Path class are syntactic, meaning that they operate on the Path instance.

License

Apache License

Parameter

Parameter Description
path The Path to be tested
options The LinkOption s to use

Return

/ or null according to the file status as explained above

Declaration

public static Boolean checkFileExists(Path path, LinkOption... options) 

Method Source Code

//package com.java2s;
/*// ww  w.jav a2  s. c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.nio.file.Files;
import java.nio.file.LinkOption;

import java.nio.file.Path;

public class Main {
    /**
     * <P>Checks if a file exists - <B>Note:</B> according to the
     * <A HREF="http://docs.oracle.com/javase/tutorial/essential/io/check.html">Java tutorial - Checking a File or Directory</A>:
     * </P>
     *
     * <PRE>
     * The methods in the Path class are syntactic, meaning that they operate
     * on the Path instance. But eventually you must access the file system
     * to verify that a particular Path exists, or does not exist. You can do
     * so with the exists(Path, LinkOption...) and the notExists(Path, LinkOption...)
     * methods. Note that !Files.exists(path) is not equivalent to Files.notExists(path).
     * When you are testing a file's existence, three results are possible:
     *
     * - The file is verified to exist.
     * - The file is verified to not exist.
     * - The file's status is unknown.
     *
     * This result can occur when the program does not have access to the file.
     * If both exists and notExists return false, the existence of the file cannot
     * be verified.
     * </PRE>
     *
     * @param path    The {@link Path} to be tested
     * @param options The {@link LinkOption}s to use
     * @return {@link Boolean#TRUE}/{@link Boolean#FALSE} or {@code null}
     * according to the file status as explained above
     */
    public static Boolean checkFileExists(Path path, LinkOption... options) {
        if (Files.exists(path, options)) {
            return Boolean.TRUE;
        } else if (Files.notExists(path, options)) {
            return Boolean.FALSE;
        } else {
            return null;
        }
    }
}

Related

  1. checkFileExists(String path)
  2. checkPathExistence(String path)
  3. directoryExist(String path)
  4. ensureDirectoryExists(final Path fileLocation)