Search in sources :

Example 26 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project applause by applause.

the class FileUtils method listFiles.

/**
 * Finds files within a given directory (and optionally its
 * subdirectories). All files found are filtered by an IOFileFilter.
 * <p>
 * If your search should recurse into subdirectories you can pass in
 * an IOFileFilter for directories. You don't need to bind a
 * DirectoryFileFilter (via logical AND) to this filter. This method does
 * that for you.
 * <p>
 * An example: If you want to search through all directories called
 * "temp" you pass in <code>FileFilterUtils.NameFileFilter("temp")</code>
 * <p>
 * Another common usage of this method is find files in a directory
 * tree but ignoring the directories generated CVS. You can simply pass
 * in <code>FileFilterUtils.makeCVSAware(null)</code>.
 *
 * @param directory  the directory to search in
 * @param fileFilter  filter to apply when finding files.
 * @param dirFilter  optional filter to apply when finding subdirectories.
 * If this parameter is <code>null</code>, subdirectories will not be included in the
 * search. Use TrueFileFilter.INSTANCE to match all directories.
 * @return an collection of java.io.File with the matching files
 * @see org.apache.commons.io.filefilter.FileFilterUtils
 * @see org.apache.commons.io.filefilter.NameFileFilter
 */
public static Collection<File> listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException("Parameter 'directory' is not a directory");
    }
    if (fileFilter == null) {
        throw new NullPointerException("Parameter 'fileFilter' is null");
    }
    // Setup effective file filter
    IOFileFilter effFileFilter = FileFilterUtils.and(fileFilter, FileFilterUtils.notFileFilter(DirectoryFileFilter.INSTANCE));
    // Setup effective directory filter
    IOFileFilter effDirFilter;
    if (dirFilter == null) {
        effDirFilter = FalseFileFilter.INSTANCE;
    } else {
        effDirFilter = FileFilterUtils.and(dirFilter, DirectoryFileFilter.INSTANCE);
    }
    // Find files
    Collection<File> files = new java.util.LinkedList<File>();
    innerListFiles(files, directory, FileFilterUtils.or(effFileFilter, effDirFilter));
    return files;
}
Also used : IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) File(java.io.File)

Example 27 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project canal by alibaba.

the class BinLogFileQueue method listBinlogFiles.

private List<File> listBinlogFiles() {
    List<File> files = new ArrayList<>();
    files.addAll(FileUtils.listFiles(directory, new IOFileFilter() {

        public boolean accept(File file) {
            Matcher matcher = binLogPattern.matcher(file.getName());
            return matcher.find();
        }

        public boolean accept(File dir, String name) {
            return true;
        }
    }, null));
    // 排一下序列
    files.sort(Comparator.comparing(File::getName));
    return files;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) File(java.io.File)

Example 28 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project symmetric-ds by JumpMind.

the class FileTrigger method createIOFileFilter.

public IOFileFilter createIOFileFilter() {
    String[] includes = StringUtils.isNotBlank(includesFiles) ? includesFiles.split(",") : new String[] { "*" };
    String[] excludes = StringUtils.isNotBlank(excludesFiles) ? excludesFiles.split(",") : null;
    IOFileFilter filter = new WildcardFileFilter(includes);
    if (excludes != null && excludes.length > 0) {
        List<IOFileFilter> fileFilters = new ArrayList<IOFileFilter>();
        fileFilters.add(filter);
        fileFilters.add(new NotFileFilter(new WildcardFileFilter(excludes)));
        filter = new AndFileFilter(fileFilters);
    }
    if (!recurse) {
        List<IOFileFilter> fileFilters = new ArrayList<IOFileFilter>();
        fileFilters.add(filter);
        fileFilters.add(new NotFileFilter(FileFilterUtils.directoryFileFilter()));
        filter = new AndFileFilter(fileFilters);
    } else {
        List<IOFileFilter> fileFilters = new ArrayList<IOFileFilter>();
        fileFilters.add(filter);
        fileFilters.add(FileFilterUtils.directoryFileFilter());
        filter = new OrFileFilter(fileFilters);
    }
    return filter;
}
Also used : AndFileFilter(org.apache.commons.io.filefilter.AndFileFilter) OrFileFilter(org.apache.commons.io.filefilter.OrFileFilter) ArrayList(java.util.ArrayList) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) NotFileFilter(org.apache.commons.io.filefilter.NotFileFilter) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter)

Example 29 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project pentaho-platform by pentaho.

the class PluginResourceLoader method findResources.

public List<URL> findResources(ClassLoader classLoader, String namePattern) {
    // $NON-NLS-1$ //$NON-NLS-2$
    String dirPattern = "", filePattern = "*";
    if (namePattern.contains("/")) {
        // $NON-NLS-1$
        String pattern = namePattern.substring(0, namePattern.lastIndexOf('/'));
        if (pattern.length() > 0) {
            dirPattern = pattern;
        }
        pattern = namePattern.substring(namePattern.lastIndexOf('/') + 1, namePattern.length());
        if (pattern.length() > 0) {
            filePattern = pattern;
        }
    } else {
        filePattern = namePattern;
    }
    IOFileFilter fileFilter = new WildcardFileFilter(filePattern);
    IOFileFilter dirFilter = TrueFileFilter.INSTANCE;
    Collection<?> files = FileUtils.listFiles(new File(getPluginDir(classLoader), dirPattern), fileFilter, dirFilter);
    Iterator<?> fileIter = files.iterator();
    List<URL> urls = new ArrayList<URL>(files.size());
    while (fileIter.hasNext()) {
        try {
            urls.add(((File) fileIter.next()).toURI().toURL());
        } catch (MalformedURLException e) {
            // $NON-NLS-1$
            Logger.warn(this, "Could not create url", e);
        }
    }
    return urls;
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) URL(java.net.URL)

Example 30 with IOFileFilter

use of org.apache.commons.io.filefilter.IOFileFilter in project pentaho-platform by pentaho.

the class PropertiesFileConfigurationTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    Properties props = new Properties();
    props.put("one", "1");
    PropertiesFileConfiguration config = new PropertiesFileConfiguration("id", props);
    final File tmpPropsFile = File.createTempFile("PropertiesFileConfiguratorTest", ".properties");
    config.setPropFile(tmpPropsFile);
    Properties newProps = new Properties();
    props.put("two", "2");
    config.update(newProps);
    Collection<File> files = FileUtils.listFiles(tmpPropsFile.getParentFile(), new IOFileFilter() {

        @Override
        public boolean accept(File file) {
            return file.getName().startsWith(tmpPropsFile.getName());
        }

        @Override
        public boolean accept(File file, String s) {
            return file.getName().startsWith(tmpPropsFile.getName());
        }
    }, null);
    assertEquals(2, files.size());
    for (File file : files) {
        // clean up the files manually. the delete on exit will only get the one, not the timestamped one created
        // in the update method
        file.deleteOnExit();
    }
}
Also used : IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Aggregations

IOFileFilter (org.apache.commons.io.filefilter.IOFileFilter)32 File (java.io.File)23 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)6 SuffixFileFilter (org.apache.commons.io.filefilter.SuffixFileFilter)4 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)4 Test (org.junit.Test)4 BatchFile (org.kuali.kfs.sys.batch.BatchFile)4 URL (java.net.URL)3 PatchFile (com.taobao.android.outputs.PatchFile)2 TpatchFile (com.taobao.android.outputs.TpatchFile)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 ZipFile (java.util.zip.ZipFile)2 AndFileFilter (org.apache.commons.io.filefilter.AndFileFilter)2 DirectoryFileFilter (org.apache.commons.io.filefilter.DirectoryFileFilter)2 NotFileFilter (org.apache.commons.io.filefilter.NotFileFilter)2 FileAlterationMonitor (org.apache.commons.io.monitor.FileAlterationMonitor)2 FileAlterationObserver (org.apache.commons.io.monitor.FileAlterationObserver)2 Ignite (org.apache.ignite.Ignite)2