Example usage for org.apache.commons.io DirectoryWalker DirectoryWalker

List of usage examples for org.apache.commons.io DirectoryWalker DirectoryWalker

Introduction

In this page you can find the example usage for org.apache.commons.io DirectoryWalker DirectoryWalker.

Prototype

protected DirectoryWalker() 

Source Link

Document

Construct an instance with no filtering and unlimited depth.

Usage

From source file:it.tidalwave.imageio.example.stats.FocalLengthStats.java

public static void main(final String[] args) {
    try {/*www .jav a2 s.co m*/
        final PrintWriter out = new PrintWriter(new File(args[1]));
        new DirectoryWalker() {
            @Override
            protected void handleFile(final File file, final int depth, final Collection results)
                    throws IOException {
                if (file.getName().toUpperCase().endsWith(".NEF")) {
                    System.out.printf("Processing %s...\n", file.getCanonicalPath());
                    final ImageReader reader = (ImageReader) ImageIO.getImageReaders(file).next();
                    reader.setInput(ImageIO.createImageInputStream(file));
                    final IIOMetadata metadata = reader.getImageMetadata(0);
                    final NEFMetadata nefMetadata = (NEFMetadata) metadata;
                    final IFD exifIFD = nefMetadata.getExifIFD();
                    final TagRational focalLength = exifIFD.getFocalLength();
                    out.println(focalLength.doubleValue());
                }
            }

            public void start() throws IOException {
                super.walk(new File(args[0]), new ArrayList<Object>());
            }
        }.start();

        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ezbake.deployer.impl.Files.java

public static void walkFileTree(final File gitRepoPath, final FileVisitor<File> simpleFileVisitor)
        throws IOException {
    new DirectoryWalker<File>() {
        @Override//ww  w. j av  a  2  s.c o m
        protected boolean handleDirectory(File directory, int depth, Collection<File> results)
                throws IOException {
            FileVisitResult x = simpleFileVisitor.preVisitDirectory(directory,
                    new BasicFileAttributes(directory));
            if (x == FileVisitResult.CONTINUE)
                return true;
            else if (x == FileVisitResult.SKIP_SUBTREE || x == FileVisitResult.SKIP_SIBLINGS)
                return false;
            else
                throw new CancelException(directory, depth);
        }

        @Override
        protected void handleFile(File file, int depth, Collection<File> results) throws IOException {
            simpleFileVisitor.visitFile(file, new BasicFileAttributes(file));
            super.handleFile(file, depth, results);
        }

        @Override
        protected void handleDirectoryEnd(File directory, int depth, Collection<File> results)
                throws IOException {
            simpleFileVisitor.postVisitDirectory(directory, null);
            super.handleDirectoryEnd(directory, depth, results);
        }

        protected void perform() throws IOException {
            walk(gitRepoPath, Lists.<File>newArrayList());
        }
    }.perform();
}

From source file:org.jboss.loom.utils.Utils.java

/**
 *  Searches a file of given name under given directory tree.
 *  @throws  CopyException if nothing found.
 *///from  w  w w  . j  av a 2s . c o  m
public static List<File> searchForFileOrDir(final String name, final File dir) throws IOException {

    List<File> found = new DirectoryWalker() {
        @Override
        protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {
            if (directory.getName().equals(name))
                results.add(directory);
            return true;
        }

        @Override
        protected void handleFile(File file, int depth, Collection results) throws IOException {
            if (file.getName().equals(name))
                results.add(file);
        }

        public List<File> search() throws IOException {
            List<File> found = new LinkedList();
            try {
                this.walk(dir, found);
            } catch (IOException ex) {
                throw new IOException("Failed traversing directory '" + dir.getAbsolutePath()
                        + "' when looking for '" + name + "'");
            }
            return found;
        }
    }.search();

    if (found.isEmpty()) {
        throw new FileNotFoundException("File '" + name + "' was not found in " + dir.getAbsolutePath());
    }
    return found;
}