Skip to content

Commit

Permalink
+FileUtils.listFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinFactory committed Jan 6, 2020
1 parent 103b969 commit bcea3c9
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/de/leonhard/storage/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,43 @@
@UtilityClass
public class FileUtils {

// ----------------------------------------------------------------------------------------------------
// Getting Files
// ----------------------------------------------------------------------------------------------------

public List<File> listFiles(final File folder) {
return listFiles(folder, null);
}

/**
* Returns a List of Files in a folder which
* ends with a specific extension.
* <p>
* If there are no files in the folder or the folder is not found,
* an empty list is returned instead of null.
*
* @param folder Folder to search in.
* @param extension Extension to search for. Set to null to skip extension validation.
*/
public List<File> listFiles(final File folder, final String extension) {
final List<File> result = new ArrayList<>();

final File[] files = folder.listFiles();

if (files == null) {
return result;
}

for (final File file : files) {
//if the extension is null we always add the file.
if (extension == null || file.getName().endsWith(extension)) {
result.add(file);
}
}

return result;
}

public File getAndMake(final String name, final String path) {
Valid.notNull(name);
Valid.notNull(path);
Expand Down

0 comments on commit bcea3c9

Please sign in to comment.