Java File Exist fileExists(File path)

Here you can find the source of fileExists(File path)

Description

Check whether a file exists.

License

Open Source License

Parameter

Parameter Description
path the whole file path

Return

true, if the file exists, false otherwise

Declaration

public static boolean fileExists(File path) 

Method Source Code

//package com.java2s;
/*//from  w w w .jav a 2s  .c  o m
 * Copyright (C) 2015 Dieter J Kybelksties
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @date: 2015-12-16
 * @author: Dieter J Kybelksties
 */

import java.io.File;

public class Main {
    /**
     * Check whether a file exists.
     *
     * @param path the whole file path
     * @return true, if the file exists, false otherwise
     */
    public static boolean fileExists(File path) {
        return path != null && path.exists() && !path.isDirectory();
    }

    /**
     * Check whether a file exists.
     *
     * @param pathString the whole file path as string
     * @return true, if the file exists, false otherwise
     */
    public static boolean fileExists(String pathString) {
        if (pathString == null) {
            return false;
        }
        File path = new File(pathString);
        return path.exists() && !path.isDirectory();
    }

    /**
     * Check whether a path (file or directory) exists.
     *
     * @param path the whole path
     * @return true, if the path exists, false otherwise
     */
    public static boolean exists(File path) {
        return path != null && path.exists();
    }

    /**
     * Check whether a path (file or directory) exists.
     *
     * @param pathString the whole path as string
     * @return true, if the path exists, false otherwise
     */
    public static boolean exists(String pathString) {
        if (pathString == null) {
            return false;
        }
        return new File(pathString).exists();
    }
}

Related

  1. fileExist(String p_filename)
  2. fileExist(String path)
  3. fileExists(File dir, String regex)
  4. fileExists(File directory, String fileName)
  5. fileExists(File file)
  6. fileExists(final String path)
  7. fileExists(final String path)
  8. fileExists(final String pathname)
  9. fileExists(String absolutePathAndFileName)