Skip to content

Instantly share code, notes, and snippets.

@ymtky ymtky/ReosurcesGetter.java
Last active Jan 16, 2018

Embed
What would you like to do?
Javaでresources化のファイルを取得する
public final class ReosurcesGetter {
public List<File> get(String path, String extension) {
File rootFile = new File(getClass().getResource(path).getPath());
return getRecursiveFiles(rootFile, extension);
}
// ファイルを再帰的に取得
private List<File> getRecursiveFiles(File file, String extension) {
Optional<File[]> maybeFiles = Optional.ofNullable(file.listFiles());
List<File> result = new ArrayList<>();
maybeFiles.ifPresent(files -> {
Arrays.asList(files)
.stream()
.filter(f -> f.isFile())
.filter(f -> hasTargetExtention(f.getName(), extension))
.forEach(f -> result.add(f));
Arrays.asList(files)
.stream()
.filter(f -> f.isDirectory())
.forEach(f -> result.addAll(getRecursiveFiles(f, extension)));
});
return result;
}
private boolean hasTargetExtention(String fineName, String extension) {
String[] arr = fineName.split("\\.");
return arr[arr.length - 1].equals(extension);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.