英文:
Java to get a list of folders which contains also at least one file
问题
这里有一个目录结构,我需要列出所有至少包含一个文件的文件夹。因此,当一个文件夹只包含子文件夹时,不应列出该文件夹。
我尝试使用以下代码,但输出中包含空文件夹。
Files.walk(Paths.get("C://testfolderstruct")).filter(Files::isDirectory).filter(Files::exists).forEach(System.out::println);
文件夹结构:
C:.
└───T1
├───T2
└───T3
test.txt
预期输出:
C:\_privat\teszt\T1\T3
英文:
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
答案1
得分: 2
Files.exists()仅检查给定路径是否存在,但不检查其是否包含文件。您需要获取路径中的文件列表。尝试像这样做:
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;
}
}
英文:
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;
}
}
答案2
得分: 0
对于递归的空目录:(a/
只有 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);
只需收集常规文件路径,并在 postVisitDirectory 中检查是否添加目录。
英文:
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.
答案3
得分: 0
这里还有一个使用NIO的文件查找示例:
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE, (path, attr) -> !attr.isDirectory())) {
stream.map(Path::getParent).distinct().forEach(System.out::println);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论