1List<String> results = new ArrayList<String>();
2
3
4File[] files = new File("/path/to/the/directory").listFiles();
5//If this pathname does not denote a directory, then listFiles() returns null.
6
7for (File file : files) {
8 if (file.isFile()) {
9 results.add(file.getName());
10 }
11}
1public class Pathnames {
2
3 public static void main(String[] args) {
4 // Creates an array in which we will store the names of files and directories
5 String[] pathnames;
6
7 // Creates a new File instance by converting the given pathname string
8 // into an abstract pathname
9 File f = new File("D:/Programming");
10
11 // Populates the array with names of files and directories
12 pathnames = f.list();
13
14 // For each pathname in the pathnames array
15 for (String pathname : pathnames) {
16 // Print the names of files and directories
17 System.out.println(pathname);
18 }
19 }
20}
21
1 public static List<String> mapFolder(String path, boolean includeEmptyFolders) {
2 List<String> map = new ArrayList<String>();
3 List<String> unmappedDirs = new ArrayList<String>();
4 File[] items = new File(path).listFiles();
5
6 if (!path.substring(path.length() - 1).equals("/")) {
7 path += "/";
8 }
9
10 if (items != null) {
11 for (File item : items) {
12 if (item.isFile())
13 map.add(path+item.getName());
14 else
15 unmappedDirs.add(path+item.getName());
16 }
17
18 if (!unmappedDirs.isEmpty()) {
19 for (String folder : unmappedDirs) {
20 List<String> temp = mapFolder(folder, includeEmptyFolders);
21 if (!temp.isEmpty()) {
22 for (String item : temp)
23 map.add(item);
24 } else if (includeEmptyFolders == true)
25 map.add(folder+"/");
26 }
27 }
28 }
29 return map;
30 }