Example usage for java.io FilenameFilter accept

List of usage examples for java.io FilenameFilter accept

Introduction

In this page you can find the example usage for java.io FilenameFilter accept.

Prototype

boolean accept(File dir, String name);

Source Link

Document

Tests if a specified file should be included in a file list.

Usage

From source file:org.openflexo.toolbox.FileUtils.java

public static Vector<File> listFilesRecursively(File dir, final FilenameFilter filter) {
    if (!dir.isDirectory()) {
        return null;
    }//from w  ww.  j  a v a 2s  . com
    Vector<File> files = new Vector<File>();
    File[] f = dir.listFiles();
    for (int i = 0; i < f.length; i++) {
        File file = f[i];
        if (file.isDirectory()) {
            files.addAll(listFilesRecursively(file, filter));
        } else if (filter.accept(dir, file.getName())) {
            files.add(file);
        }
    }
    return files;
}

From source file:org.geoserver.importer.VFSWorker.java

@SuppressWarnings("unchecked")
public Collection<File> listFilesInFolder(final File targetFolder, final FilenameFilter fileNameFilter) {
    IOFileFilter fileFilter = new IOFileFilter() {

        public boolean accept(File dir, String name) {
            return fileNameFilter.accept(dir, name);
        }/*from   ww  w  .  ja  v  a  2s. c om*/

        public boolean accept(File file) {
            return fileNameFilter.accept(file.getParentFile(), file.getName());
        }
    };
    IOFileFilter dirFilter = TrueFileFilter.INSTANCE;
    Collection<File> listFiles = FileUtils.listFiles(targetFolder, fileFilter, dirFilter);
    return listFiles;
}

From source file:org.mule.transport.ftp.FtpMessageRequester.java

protected FTPFile findFileToProcess(FTPClient client) throws Exception {
    FTPListParseEngine engine = client.initiateListParsing();
    FTPFile[] files = null;//from  www  . j  a v a 2 s.c  om
    while (engine.hasNext()) {
        files = engine.getNext(FTP_LIST_PAGE_SIZE);
        if (files == null) {
            break;
        }
        FilenameFilter filenameFilter = getFilenameFilter();
        for (int i = 0; i < files.length; i++) {
            FTPFile file = files[i];
            if (file.isFile()) {
                if (filenameFilter.accept(null, file.getName())) {
                    if (connector.validateFile(file)) {
                        // only read the first one
                        return file;
                    }
                }
            }
        }
    }
    if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
        throw new IOException("Ftp error: " + client.getReplyCode());
    }

    return null;
}

From source file:org.springframework.data.keyvalue.riak.core.io.RiakFile.java

@Override
public String[] list(FilenameFilter filenameFilter) {
    List<String> uris = new LinkedList<String>();
    for (String s : list()) {
        if (filenameFilter.accept(this, s)) {
            uris.add(s);//from w  w w.  j a  va  2  s  .  c  o  m
        }
    }
    return (String[]) uris.toArray();
}

From source file:org.apache.hyracks.storage.am.lsm.common.impls.AbstractLSMIndexFileManager.java

protected FilenameFilter getCompoundFilter(final FilenameFilter filter1, final FilenameFilter filter2) {
    return new FilenameFilter() {
        @Override//  ww  w  .  ja  v  a  2 s .c  om
        public boolean accept(File dir, String name) {
            return (filter1.accept(dir, name) && filter2.accept(dir, name));
        }
    };
}

From source file:pt.webdetails.cdf.dd.render.components.ComponentManager.java

private List<File> listAllFiles(File dir, FilenameFilter filter) {
    ArrayList<File> results = new ArrayList<File>();

    File[] files = dir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory()) {
                results.addAll(listAllFiles(file, filter));
            } else if (filter.accept(dir, file.getName())) {
                results.add(file);//from   w w w .  ja  va2  s  .c o  m
            }
        }
    }
    return results;
}

From source file:org.hyperic.hq.product.RtPlugin.java

protected ParsedFile[] generateFileList(Properties alreadyParsedFiles, String logdir, String logmask)
        throws IOException {
    FilenameFilter filter = new GlobFilenameFilter(logmask.trim());
    ArrayList removedFiles = new ArrayList();
    Enumeration en = alreadyParsedFiles.keys();

    while (en.hasMoreElements()) {
        String file = (String) en.nextElement();
        File temp = new File(file);

        if (filter.accept(temp.getParentFile(), temp.getName())) {
            removedFiles.add(file);/*w ww  .  ja  va2s .c om*/
        }
    }

    File directory = new File(logdir);
    if (!directory.canRead()) {
        this.log.error("logDir (" + logdir + ") is not readable by the agent!");
    }
    File[] flist = directory.listFiles(filter);
    ArrayList toParse = new ArrayList();

    if (flist == null || flist.length == 0) {
        this.log.warn("No valid response time log files found.  " + "logDir='" + logdir + "', logMask='"
                + logmask + "'");
        return (ParsedFile[]) toParse.toArray(new ParsedFile[0]);
    }

    for (int i = 0; i < flist.length; i++) {
        Long len = new Long(flist[i].length());
        String canonPath = flist[i].getCanonicalPath();
        String value = alreadyParsedFiles.getProperty(canonPath);

        Long oldlen = (value == null) ? new Long(0) : Long.valueOf(value);

        // This file exists, remove it from the list of files
        // that have been removed.
        removedFiles.remove(canonPath);

        if (oldlen.compareTo(len) != 0) {
            this.log.debug("Adding " + canonPath + " to parse list " + "(offset=" + oldlen + ")");
            toParse.add(new ParsedFile(canonPath, oldlen.longValue()));
        }
    }

    // Remove the files that were removed since the last time we parsed
    // the logs.  The way this 'removed files' thing is implemented is
    // soo lame.
    Iterator it = removedFiles.iterator();
    while (it.hasNext()) {
        String toRemove = (String) it.next();
        this.log.debug("Removing " + toRemove + " from parse list");
        this.log.debug(toRemove);
        alreadyParsedFiles.remove(toRemove);
    }

    return (ParsedFile[]) toParse.toArray(new ParsedFile[0]);
}

From source file:fr.landel.utils.io.FileSystemUtilsTest.java

/**
 * Check {@link FileSystemUtils#createFilenameFilter}
 * /* w  w  w  .  ja  v a  2s.c  o m*/
 * @throws IOException
 *             on error
 */
@Test
public void testCreateFilenameFilter() throws IOException {
    FilenameFilter filter = FileSystemUtils.createFilenameFilter(TXT_EXT, XML_EXT);

    assertEquals(2, FileSystemUtils.listFiles(new File(CHECK_CRC32_PATH), filter).size());

    assertFalse(filter.accept(null, null));

    assertException(() -> {
        FileSystemUtils.createFilenameFilter();
        fail();
    }, IllegalArgumentException.class, ERROR_PARAM_NULL);

    assertException(() -> {
        FileSystemUtils.createFilenameFilter((String[]) null);
        fail();
    }, IllegalArgumentException.class, ERROR_PARAM_NULL);

    assertException(() -> {
        FileSystemUtils.createFilenameFilter(new String[] { "log", null });
        fail();
    }, IllegalArgumentException.class, "extensions array cannot contains 'null'");
}

From source file:org.apache.webdav.lib.WebdavFile.java

public File[] listFiles(FilenameFilter filter) {

    WebdavResource res = null;/*from   w ww  .j a v a  2s  .c o  m*/
    try {
        res = createRes();
        WebdavResource allFiles[] = res.listWebdavResources();
        if (allFiles == null)
            return null;

        ArrayList filtered = new ArrayList();
        for (int i = 0; i < allFiles.length; i++) {
            if (filter == null || filter.accept(this, allFiles[i].getDisplayName()))
                filtered.add(new WebdavFile(allFiles[i].getHttpURL()));
        }

        return toFileArray(filtered);

    } catch (Exception e) {
        throw new WebdavException(e);
    } finally {
        closeRes(res);
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestBlockReport.java

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);/* w w  w  .j  a v  a2 s  . co m*/
    }
    return ret;
}