List of usage examples for java.io FilenameFilter accept
boolean accept(File dir, String name);
From source file:fr.landel.utils.io.InternalFileSystemUtils.java
/** * Get if file matches the filter//from w w w.java 2s .c o m * * @param file * the file (required, if null returns false) * @param fileFilter * the file filter (optional) * @param filenameFilter * the filename filter (optional) * @return if filter matches */ protected static boolean matchFilter(final File file, final FileFilter fileFilter, final FilenameFilter filenameFilter) { boolean ok = false; if (file.isFile()) { if (fileFilter != null && fileFilter.accept(file)) { ok = true; } else if (filenameFilter != null && filenameFilter.accept(null, file.getName())) { ok = true; } else if (fileFilter == null && filenameFilter == null) { ok = true; } } return ok; }
From source file:net.sourceforge.pmd.util.FileUtil.java
private static List<DataSource> collect(List<DataSource> dataSources, String fileLocation, FilenameFilter filenameFilter) { File file = new File(fileLocation); if (!file.exists()) { throw new RuntimeException("File " + file.getName() + " doesn't exist"); }/*w w w . j a v a 2s . c o m*/ if (!file.isDirectory()) { if (fileLocation.endsWith(".zip") || fileLocation.endsWith(".jar")) { ZipFile zipFile; try { zipFile = new ZipFile(fileLocation); Enumeration<? extends ZipEntry> e = zipFile.entries(); while (e.hasMoreElements()) { ZipEntry zipEntry = e.nextElement(); if (filenameFilter.accept(null, zipEntry.getName())) { dataSources.add(new ZipDataSource(zipFile, zipEntry)); } } } catch (IOException ze) { throw new RuntimeException("Archive file " + file.getName() + " can't be opened"); } } else { dataSources.add(new FileDataSource(file)); } } else { // Match files, or directories which are not excluded. // FUTURE Make the excluded directories be some configurable option Filter<File> filter = new OrFilter<>(Filters.toFileFilter(filenameFilter), new AndFilter<>(Filters.getDirectoryFilter(), Filters.toNormalizedFileFilter( Filters.buildRegexFilterExcludeOverInclude(null, Collections.singletonList("SCCS"))))); FileFinder finder = new FileFinder(); List<File> files = finder.findFilesFrom(file, Filters.toFilenameFilter(filter), true); for (File f : files) { dataSources.add(new FileDataSource(f)); } } return dataSources; }
From source file:com.symbian.driver.remoting.packaging.build.PackageBuilder.java
/** * @param directory// w w w . j a v a 2s. c o m * @param filter * @param recurse * @return */ 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 (int i = 0; i < entries.length; i++) { File entry = entries[i]; // If there is no filter or the filter accepts the // file / directory, add it to the list 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()) { files.addAll(listFiles(entry, filter, recurse)); } } // Return collection of files return files; }
From source file:com.t3.model.AssetManager.java
/** * Recursively search from the rootDir, filtering files based on fileFilter, and store a reference to every file * seen./*from w w w . ja v a 2 s .c om*/ * * @param rootDir * Starting directory to recurse from * @param fileFilter * Only add references to image files that are allowed by the filter */ public static void searchForImageReferences(File rootDir, FilenameFilter fileFilter) { for (File file : rootDir.listFiles()) { if (file.isDirectory()) { searchForImageReferences(file, fileFilter); continue; } try { if (fileFilter.accept(rootDir, file.getName())) { if (TabletopTool.getFrame() != null) { TabletopTool.getFrame().setStatusMessage("Caching image reference: " + file.getName()); } rememberLocalImageReference(file); } } catch (IOException ioe) { ioe.printStackTrace(); } } // Done if (TabletopTool.getFrame() != null) { TabletopTool.getFrame().setStatusMessage(""); } }
From source file:net.rptools.maptool.model.AssetManager.java
/** * Recursively search from the rootDir, filtering files based on fileFilter, and store a reference to every file * seen.//from w ww .java 2 s .co m * * @param rootDir * Starting directory to recurse from * @param fileFilter * Only add references to image files that are allowed by the filter */ public static void searchForImageReferences(File rootDir, FilenameFilter fileFilter) { for (File file : rootDir.listFiles()) { if (file.isDirectory()) { searchForImageReferences(file, fileFilter); continue; } try { if (fileFilter.accept(rootDir, file.getName())) { if (MapTool.getFrame() != null) { MapTool.getFrame().setStatusMessage("Caching image reference: " + file.getName()); } rememberLocalImageReference(file); } } catch (IOException ioe) { ioe.printStackTrace(); } } // Done if (MapTool.getFrame() != null) { MapTool.getFrame().setStatusMessage(""); } }
From source file:org.apache.nifi.file.FileUtils.java
/** * 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//from ww w.j a v a 2s . co m * @param filter if null then no filter is used * @param logger * @param recurse * @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); } } } } }
From source file:com.bluexml.side.Integration.eclipse.branding.enterprise.actions.ModelMigrationHelper.java
public static List<File> getModels(IProject source, final String[] fileExt) { File projectHome = IFileHelper.convertIRessourceToFile(source); FilenameFilter filter = new FilenameFilter() { public boolean accept(File file, String name) { boolean ok = false; for (String string : fileExt) { if (name.endsWith(string)) { ok = true;//from w ww .j ava 2s . c o m break; } } return file.isFile() && ok; } }; List<File> files = FileHelper.listAll(projectHome); List<File> models = new ArrayList<File>(); for (File file : files) { if (filter.accept(file, file.getName())) { models.add(file); } } return models; }
From source file:marytts.util.io.FileUtils.java
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 there is no filter or the filter accepts the // file / directory, add it to the list if (filter == null || filter.accept(directory, entry.getName())) { files.add(entry);/* w w w . j a v a 2s .co m*/ } // If the file is a directory and the recurse flag // is set, recurse into the directory if (recurse && entry.isDirectory()) { files.addAll(listFiles(entry, filter, recurse)); } } // Return collection of files return files; }
From source file:org.mule.transport.ftps.FtpsMessageRequester.java
protected boolean isValid(FTPFile file, FilenameFilter filenameFilter) { return filenameFilter.accept(null, file.getName()) && connector.validateFile(file); }
From source file:org.apache.flink.yarn.YarnTestBase.java
/** * Locate a file or directory/*w ww .ja v a 2s . c o m*/ */ 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; }