0

I have the following structure:

Main folder
- Folder A
-- Sub Folder AA
--- Sub Folder AAA
----- Files
--- Sub Folder AAB
----- Files
-- Sub Folder AB
--- Sub Folder ABA
---- Sub Folder ABAA
------ Files
--- Sub Folder ABB
------ Files

I would like to get the list of AAA AAB ABAA ABB , the order is not important.

Is there any efficient way to do it?

USer22999299
  • 5,284
  • 9
  • 46
  • 78
  • 1
    so your expectation is files in subfolder. If, make use of recursion – Shriram Aug 18 '15 at 06:48
  • Is the structure static? Just step into "Main folder", "Folder A" and then for each subfolder of "Folder A", list their direct children. – Smutje Aug 18 '15 at 06:51
  • 1
    he just wants the folders' names and not the files. But recursion is still the solution – Erwan C. Aug 18 '15 at 06:52

4 Answers4

2

Iterate through you file list and use Java.io.File.isDirectory() method to check if it is a directory if it is not a directory then it should the previous folder is the least subfolder.

Check http://www.tutorialspoint.com/java/io/file_isdirectory.htm to know the function of Java.io.File.isDirectory()

Community
  • 1
  • 1
Sam
  • 1,298
  • 6
  • 30
  • 65
1

First you should iterate to get all files, after that you can use String's split method for getting the leaves folder. Sample :

public MyTest(){
  String str = "c:/a/aa/aaa/test.txt";
  String[] arr = str.split("/");
  System.out.println(arr[arr.length-2]); // print aaa
}
Rudy
  • 7,008
  • 12
  • 50
  • 85
0

Here is a code snip of what i used:

private void GetListOfFlows(String path, ArrayList<File> files) {
        File directory = new File(path);

        File[] fList = directory.listFiles();
        if(fList.length>0 && !fList[0].isFile())
            for(File x : fList)
                subFolders(x, files);
    }

private void subFolders(File file, ArrayList<File> folders) {

        File[] listFiles = file.listFiles();
        if(listFiles.length > 0 && !listFiles[0].isFile())
            for(File fileInDir : listFiles)
                subFolders(fileInDir, folders);
        else if(listFiles.length > 0 && listFiles[0].isFile())
                folders.add(file);
    }
USer22999299
  • 5,284
  • 9
  • 46
  • 78
0

You could use the java.nio.file-API (link)

(Java >=1.7)

    List<Path> subDirs = new ArrayList<>();

    Path startPath = Paths.get("your StartPath");
    try {
        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {

                //If there is no subDir --> add it to your list
                if (Files.list(dir).noneMatch(d ->Files.isDirectory(d))){
                    subDirs.add(dir);
                }           
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    //print out all Subdirs
    subDirs.forEach(System.out::println);
griFlo
  • 2,084
  • 18
  • 28