[英]How to use Java's directory stream to get files/subdirectories only within directory not other subdirectories
I'm using Java's DirectoryStream to get a list of all the files/subfolders in a directory. 我正在使用Java的DirectoryStream来获取目录中所有文件/子文件夹的列表。 However, after going through all the files and folders of the directory, my code then proceeds to go through all the subdirectories.
但是,浏览完目录中的所有文件和文件夹后,我的代码将继续浏览所有子目录。 How can I stop it from going through my sub directories as well?
如何阻止它遍历子目录?
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry : stream) {
list.add(entry.getFileName().toString());
System.out.println(entry.getFileName());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
Try: 尝试:
for (Path entry : stream) {
System.out.println(entry.getFileName() + " | " + entry.toFile().isDirectory());
if (!entry.toFile().isDirectory()) {
list.add(entry.getFileName().toString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.