Skip to content

Instantly share code, notes, and snippets.

@ymtky
Last active January 16, 2018 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymtky/2339ab263abce9cbbf93aed36f83171c to your computer and use it in GitHub Desktop.
Save ymtky/2339ab263abce9cbbf93aed36f83171c to your computer and use it in GitHub Desktop.
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