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:Main.java

public static void addFilesRecursive(List<String> files, File location, FilenameFilter filter) {
    if (!location.exists()) {
        return;//from  w w  w  . j a v  a  2 s.  c  om
    }

    if (!location.isDirectory()) {
        if (filter.accept(location.getParentFile(), location.getName())) {
            files.add(location.getAbsolutePath());
        }
    }

    // we are in a directory => add all files matching filter and then
    // recursively add all files in subdirectories
    File[] tmp = location.listFiles(filter);
    if (tmp != null) {
        for (File file : tmp) {
            files.add(file.getAbsolutePath());
        }
    }

    File[] dirs = location.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    });

    if (dirs == null) {
        return;
    }
    for (File dir : dirs) {
        addFilesRecursive(files, dir, filter);
    }
}

From source file:org.opoo.util.PathUtils.java

private static void listFilesInternal(List<File> list, File dir, FilenameFilter filter, boolean recursive) {
    String ss[] = dir.list();/*from   w w w.  j  av a  2s  . c om*/
    if (ss == null)
        return;
    for (String s : ss) {
        File f = new File(dir, s);
        if (f.isFile()) {
            if (filter == null || filter.accept(dir, s)) {
                list.add(f);
            }
        }

        if (recursive && f.isDirectory()) {
            listFilesInternal(list, f, filter, recursive);
        }
    }
}

From source file:org.owasp.dependencycheck.utils.ExtractionUtil.java

/**
 * Extracts a file from an archive (input stream) and correctly builds the directory structure.
 *
 * @param input the archive input stream
 * @param destination where to write the file
 * @param filter the file filter to apply to the files being extracted
 * @param entry the entry from the archive to extract
 * @throws ExtractionException thrown if there is an error reading from the archive stream
 */// w w  w .j  a v a2s . c  o  m
private static void extractFile(ArchiveInputStream input, File destination, FilenameFilter filter,
        ArchiveEntry entry) throws ExtractionException {
    final File file = new File(destination, entry.getName());
    if (filter.accept(file.getParentFile(), file.getName())) {
        LOGGER.debug("Extracting '{}'", file.getPath());
        FileOutputStream fos = null;
        try {
            createParentFile(file);
            fos = new FileOutputStream(file);
            IOUtils.copy(input, fos);
        } catch (FileNotFoundException ex) {
            LOGGER.debug("", ex);
            final String msg = String.format("Unable to find file '%s'.", file.getName());
            throw new ExtractionException(msg, ex);
        } catch (IOException ex) {
            LOGGER.debug("", ex);
            final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
            throw new ExtractionException(msg, ex);
        } finally {
            closeStream(fos);
        }
    }
}

From source file:com.qmetry.qaf.automation.util.FileUtil.java

public static Collection<File> getFiles(File directory, String name, StringComparator c, boolean recurse) {
    // List of files / directories
    Vector<File> files = new Vector<File>();

    // Get files / directories in the directory
    File[] entries = directory.listFiles();
    FilenameFilter filter = getFileFilterFor(name, c);
    // Go over entries
    for (File entry : entries) {
        if ((filter == null) || filter.accept(directory, entry.getName())) {
            files.add(entry);//ww w  .  j  ava2 s .c o  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:com.qmetry.qaf.automation.util.FileUtil.java

public static Collection<File> getFiles(File directory, String extension, boolean recurse) {
    // List of files / directories
    Vector<File> files = new Vector<File>();

    // Get files / directories in the directory
    File[] entries = directory.listFiles();
    FilenameFilter filter = getFileFilterFor(extension, StringComparator.Suffix);
    // Go over entries
    for (File entry : entries) {
        if ((filter == null) || filter.accept(directory, entry.getName())) {
            files.add(entry);/*  ww  w  . j  ava  2  s  .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:com.codenvy.commons.lang.TarUtils.java

private static void addDirectoryRecursively(TarArchiveOutputStream tarOut, String parentPath, File dir,
        long modTime, FilenameFilter filter) throws IOException {
    final int parentPathLength = parentPath.length() + 1;
    final LinkedList<File> q = new LinkedList<>();
    q.add(dir);//from  ww w  . ja v a  2s .c  om
    while (!q.isEmpty()) {
        final File current = q.pop();
        final File[] list = current.listFiles();
        if (list != null) {
            for (File f : list) {
                if (filter.accept(current, f.getName())) {
                    final String entryName = f.getAbsolutePath().substring(parentPathLength).replace('\\', '/');
                    if (f.isDirectory()) {
                        addDirectoryEntry(tarOut, entryName, f, modTime);
                        q.push(f);
                    } else if (f.isFile()) {
                        addFileEntry(tarOut, entryName, f, modTime);
                    }
                }
            }
        }
    }
}

From source file:com.qmetry.qaf.automation.util.FileUtil.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 ((filter == null) || filter.accept(directory, entry.getName())) {
            files.add(entry);//from   w w  w .  java 2  s . com
        }

        // 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;
}

From source file:jenkins.plugins.coverity.CoverityUtils.java

public static Collection<File> listFiles(File directory, FilenameFilter filter, boolean recurse) {
    Vector<File> files = new Vector<File>();
    File[] entries = directory.listFiles();
    if (entries == null) {
        return files;
    }//from   www . ja  v a 2 s  .  c  om

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

        if (recurse && entry.isDirectory()) {
            files.addAll(listFiles(entry, filter, recurse));
        }
    }

    return files;
}

From source file:com.buaa.cfs.utils.IOUtils.java

/**
 * Return the complete list of files in a directory as strings.<p/>
 * <p>//from w  w  w  .j av  a  2  s .  c o  m
 * This is better than File#listDir because it does not ignore IOExceptions.
 *
 * @param dir    The directory to list.
 * @param filter If non-null, the filter to use when listing this directory.
 *
 * @return The list of files in the directory.
 *
 * @throws IOException On I/O error
 */
public static List<String> listDirectory(File dir, FilenameFilter filter) throws IOException {
    ArrayList<String> list = new ArrayList<String>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir.toPath())) {
        for (Path entry : stream) {
            String fileName = entry.getFileName().toString();
            if ((filter == null) || filter.accept(dir, fileName)) {
                list.add(fileName);
            }
        }
    } catch (DirectoryIteratorException e) {
        throw e.getCause();
    }
    return list;
}

From source file:nl.knaw.dans.common.lang.file.UnzipUtil.java

private static boolean createPath(final String basePathStr, final String path, final List<File> files,
        final FilenameFilter filter) throws IOException {
    final File basePath = new File(basePathStr);
    if (!basePath.exists())
        throw new FileNotFoundException(basePathStr);

    final String[] extPaths = path.split(File.separator);
    String pathStr = basePath.getPath();
    if (!pathStr.endsWith(File.separator))
        pathStr += File.separator;

    for (final String pathPiece : extPaths) {
        if (!filter.accept(basePath, pathPiece))
            return false;
    }/*from w w w .j  a va2  s.  c  o  m*/

    for (int i = 0; i < extPaths.length; i++) {
        pathStr += extPaths[i] + File.separator;
        final File npath = new File(pathStr);
        if (!npath.isDirectory()) {
            if (!npath.mkdir())
                throw new IOException("Error while creating directory " + npath.getAbsolutePath());
            files.add(npath);
        }
    }

    return true;
}