plaidshirt
plaidshirt

Reputation: 5671

Java to get a list of folders which contains also at least one file

There is a directory structure, from which I need to list all the folders, which contains at least one file. So when a folder contains just subfolders, it shouldn't be listed. I tried to use following code for this, but empty folders are present in output.

Files.walk(Paths.get("C://testfolderstruct")).filter(Files::isDirectory).filter(Files::exists).forEach(System.out::println);

Folder structure:

C:.
└───T1
    ├───T2
    └───T3
            test.txt

Expected output:

C:\_privat\teszt\T1\T3

Upvotes: 0

Views: 673

Answers (3)

Milgo
Milgo

Reputation: 2767

Files.exists() does only check if the given path does exist but not if it contains files. You have to get a list of files in your path. Try something like this:

public static void main(String[] args) throws IOException {
    Files.walk(Paths.get("C://testfolderstruct"))
        .filter(Files::isDirectory)
        .filter(p -> checkIfEmpty(p))
        .forEach(System.out::println);
}

private static boolean checkIfEmpty(Path directory) {
    try {
        return Files.list(directory)
                .filter(p -> !Files.isDirectory(p))
                .findAny()
                .isPresent();
    }
    catch (IOException e) {
        return false;
    }
}

Upvotes: 2

DuncG
DuncG

Reputation: 15086

There is also this using NIO files.find:

try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE, (path, attr) -> !attr.isDirectory())) {
        stream.map(Path::getParent).distinct().forEach(System.out::println);
}

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109532

For recursive empty directories: (a/ with only b/c/d/)

    Path path = Paths.get("C:\\...");

    List<Path> paths = new ArrayList<>();
    Files.walkFileTree(path, new SimpleFileVisitor<>() {
        Stack<Integer> filledStack = new Stack<>();

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                throws IOException {
            filledStack.push(paths.size());
            return super.preVisitDirectory(dir, attrs);
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc)
                throws IOException {
            int atDir = filledStack.pop();
            if (paths.size() > atDir) {
                paths.add(atDir, dir); // Insert in front.
            }
            return super.postVisitDirectory(dir, exc);
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                throws IOException {
            paths.add(file);
            return super.visitFile(file, attrs);
        }
    });
    paths.forEach(System.out::println);

Simply collect the regular file paths and on postVisitDirectory check whether to add the directory.

Upvotes: 0

Related Questions