How do I get a listing of all the files in a directory and its sub-directories?

John Zukowski

To recursively get all files under a specific directory, just keep calling listFiles() on the File that is returned, if it is a directory (isDirectory() returns true).

Here is what it looks like... From vineet bhatia

/**
* list all files in a directory and its sub-directories
* @param       directory to be scanned
* @return      vector of the files in all the sub-directories
*/
private Vector listAllFiles(File directory)
    {
        String[] fileList = null;
        Vector vectList = new Vector();

        if(directory.isDirectory())
            fileList = directory.list();
        
        String path = directory.getAbsolutePath();

        for(int i=0; i<fileList.length;i++)
        {
            File f = new File(path + File.separator + fileList[i]);
            if(!f.isDirectory())
                vectList.addElement(fileList[i]);
            else
            {
                Vector subList = listAllFiles(f);
                Enumeration enum = subList.elements();
                while(enum.hasMoreElements())
                    vectList.addElement(enum.nextElement());
            }
        }
        return vectList;
    }
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact