Java Code Examples for java.io.FilenameFilter#accept()

The following examples show how to use java.io.FilenameFilter#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtil.java    From qaf with MIT License 6 votes vote down vote up
public static Collection<File> listFiles(File directory, FilenameFilter filter, boolean recurse) {
	// List of files / directories
	Vector<File> files = new Vector<File>();

	// Get files / directories in the directory
	File[] entries = directory.listFiles();

	// Go over entries
	for (File entry : entries) {
		if ((filter == null) || filter.accept(directory, entry.getName())) {
			files.add(entry);
		}

		// If the file is a directory and the recurse flag
		// is set, recurse into the directory
		if (recurse && entry.isDirectory() && !entry.isHidden()) {
			files.addAll(listFiles(entry, filter, recurse));
		}
	}

	// Return collection of files
	return files;
}
 
Example 2
Source File: FileUtils.java    From pulsar with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes all files (not directories) in the given directory (recursive)
 * that match the given filename filter. If any file cannot be deleted then
 * this is printed at warn to the given logger.
 *
 * @param directory to delete contents of
 * @param filter if null then no filter is used
 * @param logger to notify
 * @param recurse will look for contents of sub directories.
 * @param deleteEmptyDirectories default is false; if true will delete
 * directories found that are empty
 * @throws IOException if abstract pathname does not denote a directory, or
 * if an I/O error occurs
 */
public static void deleteFilesInDirectory(
    final File directory, final FilenameFilter filter, final Logger logger,
    final boolean recurse, final boolean deleteEmptyDirectories) throws IOException {
    // ensure the specified directory is actually a directory and that it exists
    if (null != directory && directory.isDirectory()) {
        final File ingestFiles[] = directory.listFiles();
        if (ingestFiles == null) {
            // null if abstract pathname does not denote a directory, or if an I/O error occurs
            throw new IOException("Unable to list directory content in: " + directory.getAbsolutePath());
        }
        for (File ingestFile : ingestFiles) {
            boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
            if (ingestFile.isFile() && process) {
                FileUtils.deleteFile(ingestFile, logger, 3);
            }
            if (ingestFile.isDirectory() && recurse) {
                FileUtils.deleteFilesInDirectory(ingestFile, filter, logger, recurse, deleteEmptyDirectories);
                if (deleteEmptyDirectories && ingestFile.list().length == 0) {
                    FileUtils.deleteFile(ingestFile, logger, 3);
                }
            }
        }
    }
}
 
Example 3
Source File: ZKApplicationPackageTest.java    From vespa with Apache License 2.0 6 votes vote down vote up
/**
 * Same as normal listFiles, but use the filter only for normal files
 *
 * @param dir    directory to list files in
 * @param filter A FilenameFilter which decides which files in dir are listed
 * @return an array of Files
 */
protected static File[] listFiles(File dir, FilenameFilter filter) {
    File[] rawList = dir.listFiles();
    List<File> ret = new ArrayList<>();
    if (rawList != null) {
        for (File f : rawList) {
            if (f.isDirectory()) {
                ret.add(f);
            } else {
                if (filter.accept(dir, f.getName())) {
                    ret.add(f);
                }
            }
        }
    }
    return ret.toArray(new File[0]);
}
 
Example 4
Source File: FileUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 获取目录下所有符合filter的文件包括子目录
 *
 * @param dir    目录
 * @param filter 过滤器
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter) {
    if (dir == null || !isDir(dir)) return null;
    List<File> list = new ArrayList<>();
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file.getParentFile(), file.getName())) {
                list.add(file);
            }
            if (file.isDirectory()) {
                list.addAll(listFilesInDirWithFilter(file, filter));
            }
        }
    }
    return list;
}
 
Example 5
Source File: FileUtils.java    From XKnife-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 获取目录下所有符合filter的文件包括子目录
 *
 * @param dir    目录
 * @param filter 过滤器
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter) {
    if (dir == null || !isDir(dir)) return null;
    List<File> list = new ArrayList<>();
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file.getParentFile(), file.getName())) {
                list.add(file);
            }
            if (file.isDirectory()) {
                list.addAll(listFilesInDirWithFilter(file, filter));
            }
        }
    }
    return list;
}
 
Example 6
Source File: FileUtils.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes all files (not directories) in the given directory (recursive)
 * that match the given filename filter. If any file cannot be deleted then
 * this is printed at warn to the given logger.
 *
 * @param directory to delete contents of
 * @param filter if null then no filter is used
 * @param logger to notify
 * @param recurse will look for contents of sub directories.
 * @param deleteEmptyDirectories default is false; if true will delete
 * directories found that are empty
 * @throws IOException if abstract pathname does not denote a directory, or
 * if an I/O error occurs
 */
public static void deleteFilesInDirectory(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) throws IOException {
    // ensure the specified directory is actually a directory and that it exists
    if (null != directory && directory.isDirectory()) {
        final File ingestFiles[] = directory.listFiles();
        if (ingestFiles == null) {
            // null if abstract pathname does not denote a directory, or if an I/O error occurs
            throw new IOException("Unable to list directory content in: " + directory.getAbsolutePath());
        }
        for (File ingestFile : ingestFiles) {
            boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
            if (ingestFile.isFile() && process) {
                FileUtils.deleteFile(ingestFile, logger, 3);
            }
            if (ingestFile.isDirectory() && recurse) {
                FileUtils.deleteFilesInDirectory(ingestFile, filter, logger, recurse, deleteEmptyDirectories);
                if (deleteEmptyDirectories && ingestFile.list().length == 0) {
                    FileUtils.deleteFile(ingestFile, logger, 3);
                }
            }
        }
    }
}
 
Example 7
Source File: FileUtils.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes all files (not directories) in the given directory (recursive) that match the given filename filter. If any file cannot be deleted then this is printed at warn to the given logger.
 *
 * @param directory the directory to scan
 * @param filter if null then no filter is used
 * @param logger the logger
 * @param recurse whether to recurse subdirectories or not
 * @param deleteEmptyDirectories default is false; if true will delete directories found that are empty
 */
public static void deleteFilesInDir(final File directory, final FilenameFilter filter, final Logger logger, final boolean recurse, final boolean deleteEmptyDirectories) {
    // ensure the specified directory is actually a directory and that it exists
    if (null != directory && directory.isDirectory()) {
        final File ingestFiles[] = directory.listFiles();
        for (File ingestFile : ingestFiles) {
            boolean process = (filter == null) ? true : filter.accept(directory, ingestFile.getName());
            if (ingestFile.isFile() && process) {
                FileUtils.deleteFile(ingestFile, logger, 3);
            }
            if (ingestFile.isDirectory() && recurse) {
                FileUtils.deleteFilesInDir(ingestFile, filter, logger, recurse, deleteEmptyDirectories);
                if (deleteEmptyDirectories && ingestFile.list().length == 0) {
                    FileUtils.deleteFile(ingestFile, logger, 3);
                }
            }
        }
    }
}
 
Example 8
Source File: FileUtil.java    From recheck with GNU Affero General Public License v3.0 6 votes vote down vote up
public static List<File> listFilesRecursively( final File folder, final FilenameFilter filter ) {
	if ( !folder.isDirectory() ) {
		throw new IllegalArgumentException( "'" + folder + "' is not a directory!" );
	}
	final List<File> result = new ArrayList<>();
	final File[] files = folder.listFiles();
	if ( files == null ) {
		return result;
	}
	for ( final File file : files ) {
		if ( filter.accept( file, file.getName() ) ) {
			result.add( file );
		}
		if ( file.isDirectory() ) {
			result.addAll( listFilesRecursively( file, filter ) );
		}
	}
	return result;
}
 
Example 9
Source File: FileUtils.java    From AcgClub with MIT License 6 votes vote down vote up
/**
 * 获取目录下所有符合filter的文件包括子目录
 *
 * @param dir 目录
 * @param filter 过滤器
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter) {
  if (dir == null || !isDir(dir)) {
    return null;
  }
  List<File> list = new ArrayList<>();
  File[] files = dir.listFiles();
  if (files != null && files.length != 0) {
    for (File file : files) {
      if (filter.accept(file.getParentFile(), file.getName())) {
        list.add(file);
      }
      if (file.isDirectory()) {
        list.addAll(listFilesInDirWithFilter(file, filter));
      }
    }
  }
  return list;
}
 
Example 10
Source File: FileUtils.java    From AcgClub with MIT License 6 votes vote down vote up
/**
 * 获取目录下所有符合filter的文件
 *
 * @param dir 目录
 * @param filter 过滤器
 * @param isRecursive 是否递归进子目录
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter,
    boolean isRecursive) {
  if (isRecursive) {
    return listFilesInDirWithFilter(dir, filter);
  }
  if (dir == null || !isDir(dir)) {
    return null;
  }
  List<File> list = new ArrayList<>();
  File[] files = dir.listFiles();
  if (files != null && files.length != 0) {
    for (File file : files) {
      if (filter.accept(file.getParentFile(), file.getName())) {
        list.add(file);
      }
    }
  }
  return list;
}
 
Example 11
Source File: OrFilenameFilter.java    From ModTheSpire with MIT License 6 votes vote down vote up
/**
 * <p>Determine whether a file is to be accepted or not, based on the
 * contained filters. The file is accepted if any one of the contained
 * filters accepts it. This method stops looping over the contained
 * filters as soon as it encounters one whose <tt>accept()</tt> method
 * returns <tt>true</tt> (implementing a "short-circuited OR"
 * operation.)</p>
 *
 * <p>If the set of contained filters is empty, then this method
 * returns <tt>true</tt>.</p>
 *
 * @param dir   The directory containing the file.
 * @param name  the file name
 *
 * @return <tt>true</tt> if the file matches, <tt>false</tt> if it doesn't
 */
public boolean accept (File dir, String name)
{
    boolean accepted = false;

    if (filters.size() == 0)
        accepted = true;

    else
    {
        for (FilenameFilter filter : filters)
        {
            accepted = filter.accept (dir, name);
            if (accepted)
                break;
        }
    }

    return accepted;
}
 
Example 12
Source File: FileUtil.java    From Sandbox with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static Collection<File> getFiles(File folder, FilenameFilter fileFilter, boolean recursive) {
    File[] files = folder.listFiles();
    if (files == null)
        return Collections.emptySet();
    Set<File> allFiles = new HashSet<>();
    for (File f : files) {
        if (recursive && f.isDirectory()) {
            allFiles.addAll(getFiles(f, fileFilter, true));
            continue;
        }
        if (fileFilter.accept(folder, f.getName())) {
            allFiles.add(f);
        }
    }
    return allFiles;
}
 
Example 13
Source File: SimpleFileFinder.java    From synopsys-detect with Apache License 2.0 6 votes vote down vote up
private List<File> findFiles(final File directoryToSearch, final FilenameFilter filenameFilter, final int depth, final boolean findInsideMatchingDirectories) {
    final List<File> foundFiles = new ArrayList<>();
    if (depth < 0) {
        return foundFiles;
    }
    if (Files.isSymbolicLink(directoryToSearch.toPath())) {
        return foundFiles;
    }
    final File[] allFiles = directoryToSearch.listFiles();
    if (allFiles == null) {
        return foundFiles;
    }
    for (final File file : allFiles) {
        final boolean matches = filenameFilter.accept(directoryToSearch, file.getName());
        if (matches) {
            foundFiles.add(file);
        }
        if (!matches || findInsideMatchingDirectories) {
            if (file.isDirectory() && !Files.isSymbolicLink(file.toPath())) {
                foundFiles.addAll(findFiles(file, filenameFilter, depth - 1, findInsideMatchingDirectories));
            }
        }
    }

    return foundFiles;
}
 
Example 14
Source File: YarnTestBase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Locate a file or directory.
 */
public static File findFile(String startAt, FilenameFilter fnf) {
	File root = new File(startAt);
	String[] files = root.list();
	if (files == null) {
		return null;
	}
	for (String file : files) {
		File f = new File(startAt + File.separator + file);
		if (f.isDirectory()) {
			File r = findFile(f.getAbsolutePath(), fnf);
			if (r != null) {
				return r;
			}
		} else if (fnf.accept(f.getParentFile(), f.getName())) {
			return f;
		}
	}
	return null;
}
 
Example 15
Source File: FileUtils.java    From XKnife-Android with Apache License 2.0 5 votes vote down vote up
/**
 * 获取目录下所有符合filter的文件
 *
 * @param dir         目录
 * @param filter      过滤器
 * @param isRecursive 是否递归进子目录
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter, boolean isRecursive) {
    if (isRecursive) return listFilesInDirWithFilter(dir, filter);
    if (dir == null || !isDir(dir)) return null;
    List<File> list = new ArrayList<>();
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file.getParentFile(), file.getName())) {
                list.add(file);
            }
        }
    }
    return list;
}
 
Example 16
Source File: FileUtils.java    From Android-UtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 获取目录下所有符合filter的文件
 *
 * @param dir         目录
 * @param filter      过滤器
 * @param isRecursive 是否递归进子目录
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter, boolean isRecursive) {
    if (isRecursive) return listFilesInDirWithFilter(dir, filter);
    if (dir == null || !isDir(dir)) return null;
    List<File> list = new ArrayList<>();
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file.getParentFile(), file.getName())) {
                list.add(file);
            }
        }
    }
    return list;
}
 
Example 17
Source File: RxFileTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * 获取目录下所有符合filter的文件
 *
 * @param dir         目录
 * @param filter      过滤器
 * @param isRecursive 是否递归进子目录
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(File dir, FilenameFilter filter, boolean isRecursive) {
    if (isRecursive) return listFilesInDirWithFilter(dir, filter);
    if (dir == null || !isDir(dir)) return null;
    List<File> list = new ArrayList<>();
    File[] files = dir.listFiles();
    for (File file : files) {
        if (filter.accept(file.getParentFile(), file.getName())) {
            list.add(file);
        }
    }
    return list;
}
 
Example 18
Source File: BlockReportTestBase.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private List<File> findAllFiles(File top, FilenameFilter mask) {
  if (top == null) return null;
  ArrayList<File> ret = new ArrayList<File>();
  for (File f : top.listFiles()) {
    if (f.isDirectory())
      ret.addAll(findAllFiles(f, mask));
    else if (mask.accept(f, f.getName()))
      ret.add(f);
  }
  return ret;
}
 
Example 19
Source File: BlockReportTestBase.java    From big-c with Apache License 2.0 5 votes vote down vote up
private List<File> findAllFiles(File top, FilenameFilter mask) {
  if (top == null) return null;
  ArrayList<File> ret = new ArrayList<File>();
  for (File f : top.listFiles()) {
    if (f.isDirectory())
      ret.addAll(findAllFiles(f, mask));
    else if (mask.accept(f, f.getName()))
      ret.add(f);
  }
  return ret;
}
 
Example 20
Source File: AndFilenameFilter.java    From ModTheSpire with MIT License 3 votes vote down vote up
/**
 * <p>Determine whether a file is to be accepted or not, based on the
 * contained filters. The file is accepted if any one of the contained
 * filters accepts it. This method stops looping over the contained
 * filters as soon as it encounters one whose <tt>accept()</tt> method
 * returns <tt>false</tt> (implementing a "short-circuited AND"
 * operation.)</p>
 *
 * <p>If the set of contained filters is empty, then this method
 * returns <tt>true</tt>.</p>
 *
 * @param dir   The directory containing the file.
 * @param name  the file name
 *
 * @return <tt>true</tt> if the file matches, <tt>false</tt> if it doesn't
 */
public boolean accept (File dir, String name)
{
    boolean accepted = true;

    for (FilenameFilter filter : filters)
    {
        accepted = filter.accept (dir, name);
        if (! accepted)
            break;
    }

    return accepted;
}