-1

I have series of folders, inside which there are files to be processed.Inside each folder, there are maybe other sub-folders as well, which are to be scanned. I wish to know if there is any way to do that. Whatever open-source code I could find, they were all for scanning only 1 folder.

The directory structure (rough sketch) in my case is as follows: enter image description here

So far, I just got this:

public static void directory(File dir){
File[] files = dir.listFiles();
for(File file:files){
    System.out.println(file.getAbsolutePath());
    if(file.listFiles() != null)
        directory(file);        
}}

Please Help !

Arnab
  • 63
  • 3
  • 14
  • 1
    Please show what you have tried so far – fdsa Aug 20 '15 at 20:31
  • possible duplicate of http://stackoverflow.com/questions/2056221/recursively-list-files-in-java – Filipp Voronov Aug 20 '15 at 20:32
  • So what exactly is the problem with what you have so far? – Mureinik Aug 20 '15 at 20:33
  • "*I wish to know if there is any way to do that*" yes is a way. Try recursion like `scan(File f){ for each element in directory (if element is directory){scan(element)} else {handle(element)}}`. – Pshemo Aug 20 '15 at 20:34
  • the above link is for listing files in a single directory.. I have multiple directories, and that's where I am stuck.. – Arnab Aug 20 '15 at 20:35
  • @Mureinik right now, I am only able to scan only one directory, I cannot go inside the sub-directories. – Arnab Aug 20 '15 at 20:37
  • since Java 7 http://stackoverflow.com/a/3154643/1393766, since Java 8 http://stackoverflow.com/a/24006711/1393766 – Pshemo Aug 20 '15 at 20:48

1 Answers1

0

You can have something like this.

public void listfiles(String directoryURL, List<File> fileList) {
    File directory = new File(directoryURL);
    File[] fileArray = directory.listFiles();
    for (File file : fileArray) {
        if (file.isFile()) {
            fileList.add(file);
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath(), fileList);
        }
    }
}
Aksanth
  • 269
  • 2
  • 13