Code example for File

Methods: exists, isDirectory, isFile

0
     * @throws Exception When the file does not exist or is not 
     *         the type specified. 
     */ 
    public static File checkFile(File file, boolean isDir)
        throws FileNotFoundException { 
        if (!file.exists()) {
            throw new FileNotFoundException((isDir ? "Folder" : "File") + " does not exist: "
                    + file);
        } 
 
        if (isDir && !file.isDirectory()) {
            throw new FileNotFoundException("File exists but is not a directory:" + file);
        } 
 
        if (!isDir && !file.isFile()) {
            //may it be some sort of OS special file (e.g. /dev/tty1) 
            throw new FileNotFoundException("File exists but is not a regular file:" + file);
        } 
 
        if (LOGGER.isLoggable(Level.FINER)) {
            LOGGER.finer(new StringBuffer("File is valid: ").append(file).toString());
        } 
 
        return file;
    }