我正在尝试获取.java-files
(已给出)中的所有directory
及其所有子目录。我就是这样:
public static void getJavaFiles(Path path) {
DirectoryStream<Path> stream = null;
try {
stream = Files.newDirectoryStream(path);
for (Path entry : stream) {
if (Files.isRegularFile(entry)) {
if(entry.getFileName().toString() == "*.java") {
System.out.println(entry.getFileName().toString());
};
} else if (Files.isDirectory(entry)) {
getJavaFiles(entry);
}
}
} catch (IOException e) {
throw new RuntimeException(String.format("error reading folder %s: %s", path, e.getMessage()), e);
} finally {
if(stream != null) {
try {
stream.close();
} catch (IOException e) {
}
}
}
}
不幸的是,entry.getFileName().toString() == "*.java"
并没有按照我的预期工作。我获取了所有文件,但是如何仅获取.java
文件?
答案 0 :(得分:1)
您可以检查以下内容:
if(entry.getFileName().endsWith(".java"))