-2

i have same folder name in several folders i want to find the all folders in i ll get the path like this. in properties file \usr\local\*\test*\fol* using java how to find this i have seen some apache WildcardFileFilter class but this is not working, in java there is some thing called PathMatcher. is this works for me please help me

Gaali Prabhakar
  • 583
  • 6
  • 23

1 Answers1

0

Assuming you have following directory structure

/tmp/usr/local/bar/test1/fol1
/tmp/usr/local/baz/test2/fol2
/tmp/usr/local/foo/test3/fol3
/tmp/usr/local/foobar/test/fol
/tmp/usr/local/foobar/test/dummy
/tmp/usr/local/foobar/dummy

A possible solution for Java 7+ might be

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import static java.nio.file.FileVisitResult.CONTINUE;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

public class FindWalkFileTree {

    public static class Finder implements FileVisitor<Path> {
        private final PathMatcher matcher;

        Finder(String pattern) {
            final FileSystem fs = FileSystems.getDefault();
            matcher = fs.getPathMatcher("glob:" + pattern);
        }

        void find(Path file) {
            if (matcher.matches(file)) {
                System.out.format("%s%n", file);
            }
        }

        @Override
        public FileVisitResult visitFile(Path file, 
                BasicFileAttributes attrs) {
            find(file);
            return CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, 
                IOException exc) {
            return CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) {
            return CONTINUE;
        }

        @Override
        public FileVisitResult preVisitDirectory(Path dir, 
                BasicFileAttributes attrs) throws IOException {
            find(dir);
            return CONTINUE;
        }
    }

    public static void main(String[] args) throws IOException {
        Path searchDir = Paths.get("/tmp/usr/local");
        String pattern = "/tmp/usr/local/*/test*/fol*";
        EnumSet<FileVisitOption> opts = EnumSet.of(
                FileVisitOption.FOLLOW_LINKS);
        Finder finder = new Finder(pattern);
        Files.walkFileTree(searchDir, opts, Integer.MAX_VALUE, finder);
    }
}

A possible Java 8 solution might be

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.util.stream.Stream;

public class FindWalk {

    public static void main(String[] args) throws IOException {
        File dataDir = new File("/tmp/usr/local");
        FileSystem fs = FileSystems.getDefault();
        String pattern = "/tmp/usr/local/*/test*/fol*";
        PathMatcher pathMatcher = fs.getPathMatcher("glob:" + pattern);
        FileVisitOption opts = FileVisitOption.FOLLOW_LINKS;
        try (Stream<Path> stream = Files.walk(dataDir.toPath(), opts)) {
            stream.filter(pathMatcher::matches)
                    .forEach(System.out::println);
        }
    }
}

output for both solutions

/tmp/usr/local/bar/test1/fol1
/tmp/usr/local/baz/test2/fol2
/tmp/usr/local/foo/test3/fol3
/tmp/usr/local/foobar/test/fol

The processing always starts from directory /tmp/usr/local/.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69