I am trying to get all .java-files
in a directory
(that is given) and all its subdirectories . This is what Ive come up:
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) {
}
}
}
}
Unfortunately entry.getFileName().toString() == "*.java"
is not working how I thought it would. I get all the files but how do I get only the .java
files ?