Created
September 26, 2021 10:08
-
-
Save vigneshrcsengg/ff1b98587b90ac5490736b892a71bb11 to your computer and use it in GitHub Desktop.
List all files and folders from the Directory
This file contains hidden or 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
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