Skip to content

Instantly share code, notes, and snippets.

@vigneshrcsengg
Created September 26, 2021 10:08
Show Gist options
  • Save vigneshrcsengg/ff1b98587b90ac5490736b892a71bb11 to your computer and use it in GitHub Desktop.
Save vigneshrcsengg/ff1b98587b90ac5490736b892a71bb11 to your computer and use it in GitHub Desktop.
List all files and folders from the Directory
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class ToAbsoluteFilePath {
public static List<File> listFiles(String directoryName) {
File directory = new File(directoryName);
List<File> resultList = new ArrayList<>();
// get all the files from a directory
File[] fList = directory.listFiles();
resultList.addAll(Arrays.asList(fList));
for (File file : fList) {
if (file.isFile()) {
resultList.add(file);
} else if (file.isDirectory()) {
resultList.addAll(listFiles(file.getAbsolutePath()));
}
}
return resultList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment