Last active
January 16, 2018 05:38
-
-
Save ymtky/2339ab263abce9cbbf93aed36f83171c to your computer and use it in GitHub Desktop.
Javaでresources化のファイルを取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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