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

The following examples show how to use java.io.FileFilter#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: MockFileFinder.java    From synopsys-detect with Apache License 2.0 7 votes vote down vote up
@Override
public List<File> findFiles(final File directoryToSearch, final List<String> filenamePatterns, final int depth, boolean findInsideMatchingDirectories) {
    List<File> found = new ArrayList<>();
    for (int i = 0; i <= depth; i++) {
        if (files.containsKey(i)) {
            List<File> possibles = files.get(i);
            for (String pattern : filenamePatterns) {
                FileFilter fileFilter = new WildcardFileFilter(pattern);
                for (File possible : possibles) {
                    if (fileFilter.accept(possible)) {
                        found.add(possible);
                    }
                }

            }
        }
    }
    return found;
}
 
Example 2
Source File: FlagMaker.java    From datawave with Apache License 2.0 7 votes vote down vote up
/**
 * Determine the number of unprocessed flag files in the flag directory
 * 
 * @param fc
 * @return the flag found for this ingest pool
 */
private int countFlagFileBacklog(final FlagDataTypeConfig fc) {
    final MutableInt fileCounter = new MutableInt(0);
    final FileFilter fileFilter = new WildcardFileFilter("*_" + fc.getIngestPool() + "_" + fc.getDataName() + "_*.flag");
    final FileVisitor<java.nio.file.Path> visitor = new SimpleFileVisitor<java.nio.file.Path>() {
        
        @Override
        public FileVisitResult visitFile(java.nio.file.Path path, BasicFileAttributes attrs) throws IOException {
            if (fileFilter.accept(path.toFile())) {
                fileCounter.increment();
            }
            return super.visitFile(path, attrs);
        }
    };
    try {
        Files.walkFileTree(Paths.get(fmc.getFlagFileDirectory()), visitor);
    } catch (IOException e) {
        // unable to get a flag count....
        log.error("Unable to get flag file count", e);
        return -1;
    }
    return fileCounter.intValue();
}
 
Example 3
Source File: FileUtil.java    From java-trader with Apache License 2.0 6 votes vote down vote up
/**
 * 递归遍历所有文件
 */
public static List<File> listAllFiles(File dir, FileFilter filter) {
    List<File> result = new ArrayList<>();
    LinkedList<File> dirs = new LinkedList<>();
    dirs.add( dir );
    while(!dirs.isEmpty()) {
        File file = dirs.poll();
        if ( file.isDirectory() ) {
            File[] files = file.listFiles();
            if (files!=null) {
                dirs.addAll(Arrays.asList(files));
            }
            continue;
        }
        if ( filter==null || filter.accept(file) ) {
            result.add(file);
        }
    }
    return result;
}
 
Example 4
Source File: ListFile.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private List<FileInfo> scanDirectory(final File path, final FileFilter filter, final Boolean recurse,
                                     final Long minTimestamp) throws IOException {
    final List<FileInfo> listing = new ArrayList<>();
    File[] files = path.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory()) {
                if (recurse) {
                    listing.addAll(scanDirectory(file, filter, true, minTimestamp));
                }
            } else {
                if ((minTimestamp == null || file.lastModified() >= minTimestamp) && filter.accept(file)) {
                    listing.add(new FileInfo.Builder()
                            .directory(file.isDirectory())
                            .filename(file.getName())
                            .fullPathFileName(file.getAbsolutePath())
                            .lastModifiedTime(file.lastModified())
                            .build());
                }
            }
        }
    }

    return listing;
}
 
Example 5
Source File: FileUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
public static List<File> listFilesInDirWithFilter(final File dir, final FileFilter filter, final boolean isRecursive) {
    if (!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)) {
                list.add(file);
            }
            if (isRecursive && file.isDirectory()) {
                //noinspection ConstantConditions
                list.addAll(listFilesInDirWithFilter(file, filter, true));
            }
        }
    }
    return list;
}
 
Example 6
Source File: FileUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {
    if (dir == null) return false;
    // dir doesn't exist then return true
    if (!dir.exists()) return true;
    // dir isn't a directory then return false
    if (!dir.isDirectory()) return false;
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                if (file.isFile()) {
                    if (!file.delete()) return false;
                } else if (file.isDirectory()) {
                    if (!deleteDir(file)) return false;
                }
            }
        }
    }
    return true;
}
 
Example 7
Source File: FileUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取目录下所有过滤的文件
 * @param dir         目录
 * @param filter      过滤器
 * @param isRecursive 是否递归进子目录
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(final File dir, final FileFilter filter, final boolean isRecursive) {
    if (!isDirectory(dir) || filter == null) 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)) {
                list.add(file);
            }
            if (isRecursive && file.isDirectory()) {
                list.addAll(listFilesInDirWithFilter(file, filter, true));
            }
        }
    }
    return list;
}
 
Example 8
Source File: FileUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取目录下所有过滤的文件
 * @param dir         目录
 * @param filter      过滤器
 * @param isRecursive 是否递归进子目录
 * @return 文件链表
 */
public static List<File> listFilesInDirWithFilter(final File dir, final FileFilter filter, final boolean isRecursive) {
    if (!isDirectory(dir) || filter == null) 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)) {
                list.add(file);
            }
            if (isRecursive && file.isDirectory()) {
                list.addAll(listFilesInDirWithFilter(file, filter, true));
            }
        }
    }
    return list;
}
 
Example 9
Source File: FileUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 删除目录下所有过滤的文件
 * @param dir    目录
 * @param filter 过滤器
 * @return {@code true} 删除成功, {@code false} 删除失败
 */
public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {
    if (filter == null) return false;
    // dir is null then return false
    if (dir == null) return false;
    // dir doesn't exist then return true
    if (!dir.exists()) return true;
    // dir isn't a directory then return false
    if (!dir.isDirectory()) return false;
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                if (file.isFile()) {
                    if (!file.delete()) return false;
                } else if (file.isDirectory()) {
                    if (!deleteDir(file)) return false;
                }
            }
        }
    }
    return true;
}
 
Example 10
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 FileFilter 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 ) ) {
			result.add( file );
		}
		if ( file.isDirectory() ) {
			result.addAll( listFilesRecursively( file, filter ) );
		}
	}
	return result;
}
 
Example 11
Source File: FileUtil.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * Return the files that satisfy the filter in directory.
 *
 * @param dir The directory.
 * @param filter The filter.
 * @param isRecursive True to traverse subdirectories, false otherwise.
 * @return the files that satisfy the filter in directory
 */
public static List<File> listFilesInDirWithFilter(final File dir, final FileFilter filter,
                                                  final boolean isRecursive) {
    if (!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)) {
                list.add(file);
            }
            if (isRecursive && file.isDirectory()) {
                //noinspection ConstantConditions
                list.addAll(listFilesInDirWithFilter(file, filter, true));
            }
        }
    }
    return list;
}
 
Example 12
Source File: FileUtil.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * Delete all files that satisfy the filter in directory.
 *
 * @param dir The directory.
 * @param filter The filter.
 * @return {@code true}: success<br>{@code false}: fail
 */
public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {
    if (dir == null) return false;
    // dir doesn't exist then return true
    if (!dir.exists()) return true;
    // dir isn't a directory then return false
    if (!dir.isDirectory()) return false;
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                if (file.isFile()) {
                    if (!file.delete()) return false;
                } else if (file.isDirectory()) {
                    if (!deleteDir(file)) return false;
                }
            }
        }
    }
    return true;
}
 
Example 13
Source File: CombinationFileFilter.java    From ModTheSpire with MIT License 5 votes vote down vote up
/**
 * Determine whether a file is to be accepted or not, based on the
 * contained filters and the mode. If this object's mode mode is set to
 * {@link #AND_FILTERS}, then a file must be accepted by all contained
 * filters to be accepted. If this object's mode is set to
 * {@link #OR_FILTERS}, then a file name is accepted if any one of the
 * contained filters accepts it. If the set of contained filters is
 * empty, then this method returns <tt>false</tt>.
 *
 * @param file  The file to check for acceptance
 *
 * @return <tt>true</tt> if the file matches, <tt>false</tt> if it doesn't
 */
public boolean accept (File file)
{
    boolean              accepted = false;
    Iterator<FileFilter> it = filters.iterator();
    FileFilter           filter;  

    if (mode == AND_FILTERS)
    {
        accepted = true;

        while (accepted && it.hasNext())
        {
            filter = it.next();
            accepted = filter.accept (file);
        }
    }

    else
    {
        accepted = false;

        while ((! accepted) && it.hasNext())
        {
            filter = it.next();
            accepted = filter.accept (file);
        }
    }

    return accepted;
}
 
Example 14
Source File: DirectoryScannerConfig.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds a {@code FileFilter} from the {@link #getIncludeFiles
 * includeFiles} and {@link #getExcludeFiles excludeFiles} lists.
 * A file will be accepted if it is selected by at least one of
 * the filters in {@link #getIncludeFiles includeFiles}, and is
 * not excluded by any of the filters in {@link
 * #getExcludeFiles excludeFiles}. If there's no filter in
 * {@link #getIncludeFiles includeFiles}, then a file is accepted
 * simply if it is not excluded by any of the filters in {@link
 * #getExcludeFiles excludeFiles}.
 *
 * @return A new {@code FileFilter}  created from the current snapshot
 *         of the {@link #getIncludeFiles
 * includeFiles} and {@link #getExcludeFiles excludeFiles} lists.
 *         Later modification of these lists will not affect the
 *         returned {@code FileFilter}.
 **/
public FileFilter buildFileFilter() {
    final FileFilter[] ins = getIncludeFiles();
    final FileFilter[] outs = getExcludeFiles();
    final FileFilter filter = new FileFilter() {
        public boolean accept(File f) {
            boolean result = false;
            // If no include filter, all files are included.
            if (ins != null) {
                for (FileFilter in: ins) {
                    // if one filter accepts it, file is included
                    if (!in.accept(f)) continue;

                    // file is accepted, include it
                    result=true;
                    break;
                }
            } else result= true;
            if (result == false) return false;

            // The file is in the include list. Let's see if it's not
            // in the exclude list...
            //
            if (outs != null) {
                for (FileFilter out: outs) {
                    // if one filter accepts it, file is excluded
                    if (!out.accept(f)) continue;

                    // file is accepted, exclude it.
                    result=false;
                    break;
                }
            }
            return result;
        }
    };
    return filter;
}
 
Example 15
Source File: CompositeFilter.java    From styT with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(File f) {
    for (FileFilter filter : mFilters) {
        if (!filter.accept(f)) {
            return false;
        }
    }

    return true;
}
 
Example 16
Source File: GetFile.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private Set<File> performListing(final File directory, final FileFilter filter, final boolean recurseSubdirectories) {
    Path p = directory.toPath();
    if (!Files.isWritable(p) || !Files.isReadable(p)) {
        throw new IllegalStateException("Directory '" + directory + "' does not have sufficient permissions (i.e., not writable and readable)");
    }
    final Set<File> queue = new HashSet<>();
    if (!directory.exists()) {
        return queue;
    }

    final File[] children = directory.listFiles();
    if (children == null) {
        return queue;
    }

    for (final File child : children) {
        if (child.isDirectory()) {
            if (recurseSubdirectories) {
                queue.addAll(performListing(child, filter, recurseSubdirectories));
            }
        } else if (filter.accept(child)) {
            queue.add(child);
        }
    }

    return queue;
}
 
Example 17
Source File: DirectoryScannerConfig.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds a {@code FileFilter} from the {@link #getIncludeFiles
 * includeFiles} and {@link #getExcludeFiles excludeFiles} lists.
 * A file will be accepted if it is selected by at least one of
 * the filters in {@link #getIncludeFiles includeFiles}, and is
 * not excluded by any of the filters in {@link
 * #getExcludeFiles excludeFiles}. If there's no filter in
 * {@link #getIncludeFiles includeFiles}, then a file is accepted
 * simply if it is not excluded by any of the filters in {@link
 * #getExcludeFiles excludeFiles}.
 *
 * @return A new {@code FileFilter}  created from the current snapshot
 *         of the {@link #getIncludeFiles
 * includeFiles} and {@link #getExcludeFiles excludeFiles} lists.
 *         Later modification of these lists will not affect the
 *         returned {@code FileFilter}.
 **/
public FileFilter buildFileFilter() {
    final FileFilter[] ins = getIncludeFiles();
    final FileFilter[] outs = getExcludeFiles();
    final FileFilter filter = new FileFilter() {
        public boolean accept(File f) {
            boolean result = false;
            // If no include filter, all files are included.
            if (ins != null) {
                for (FileFilter in: ins) {
                    // if one filter accepts it, file is included
                    if (!in.accept(f)) continue;

                    // file is accepted, include it
                    result=true;
                    break;
                }
            } else result= true;
            if (result == false) return false;

            // The file is in the include list. Let's see if it's not
            // in the exclude list...
            //
            if (outs != null) {
                for (FileFilter out: outs) {
                    // if one filter accepts it, file is excluded
                    if (!out.accept(f)) continue;

                    // file is accepted, exclude it.
                    result=false;
                    break;
                }
            }
            return result;
        }
    };
    return filter;
}
 
Example 18
Source File: FileUtil.java    From Music-Player with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public static List<File> listFilesDeep(@NonNull Collection<File> files, @Nullable FileFilter fileFilter) {
    List<File> resFiles = new LinkedList<>();
    for (File file : files) {
        if (file.isDirectory()) {
            internalListFilesDeep(resFiles, file, fileFilter);
        } else if (fileFilter == null || fileFilter.accept(file)) {
            resFiles.add(file);
        }
    }
    return resFiles;
}
 
Example 19
Source File: FileCopier.java    From hedera-mirror-node with Apache License 2.0 4 votes vote down vote up
public FileCopier filterDirectories(FileFilter newDirFilter) {
    FileFilter andFilter = dirFilter == ALL_FILTER ? newDirFilter : f -> dirFilter.accept(f) || newDirFilter
            .accept(f);
    return new FileCopier(from, to, andFilter, fileFilter);
}
 
Example 20
Source File: AndFileFilter.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 file  The file to check for acceptance
 *
 * @return <tt>true</tt> if the file matches, <tt>false</tt> if it doesn't
 */
public boolean accept (File file)
{
    boolean accepted = true;

    for (FileFilter filter : filters)
    {
        accepted = filter.accept (file);
        if (! accepted)
            break;
    }

    return accepted;
}