Monday, August 25, 2014

Traverse directories recursively and get the sorted URIs

Here is the utility class which can be used to traverse directories recursively and get the sorted URIs::

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Arrays;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

import java.util.TreeSet;

 

/**

* This class DirectoryTraverser.<br/>

* It returns set of files by traversing directories recursively.

*

* @since 2014

* @author Abhinav Kumar Mishra

*/

public final class DirectoryTraverser {

 

 /**

 * Gets the file URIs. Recursively traverse each directory and uris of files.

 *

 * @param aStartingDir the a starting dir

 * @return the file listing

 * @throws FileNotFoundException the file not found exception

 */

 public static Set<File> getFileUris(final File aStartingDir)

  throws FileNotFoundException {

  checkDirectories(aStartingDir); // throw exception if not valid.

  return getUrisRecursive(aStartingDir);

 }

 

 /**

 * Gets the file uris recursively.

 *

 * @param aStartingDir the a starting dir

 * @return the file listing no sort

 * @throws FileNotFoundException the file not found exception

 */

 private static Set<File> getUrisRecursive(final File aStartingDir)

  throws FileNotFoundException {

  final Set<File> sortedSetOfFiles = new TreeSet<File>();

  final File[] filesAndDirs = aStartingDir.listFiles();

  final List<File> filesDirs = Arrays.asList(filesAndDirs);

  final Iterator<File> filesDirsItr = filesDirs.iterator();

  while (filesDirsItr.hasNext()) {

   final File file = filesDirsItr.next();

   sortedSetOfFiles.add(file); // Add files and directory URIs both

   // If uri is a directory the revisit it recursively.

   if (!file.isFile()) {

    // Call 'getUrisRecursive' to extract uris from directory

    final Set<File> innerSet = getUrisRecursive(file);

    sortedSetOfFiles.addAll(innerSet);

   }

  }

  return sortedSetOfFiles;

 }

 

 /**

 * Checks if is valid directory.<br/>

 * If directory exists then it is valid. If directory is valid then it can

 * be read.

 *

 * @param directoryUri the a directory

 * @throws FileNotFoundException

  */

 private static void checkDirectories(final File directoryUri)

                            throws FileNotFoundException {

  if (directoryUri == null) {

   throw new IllegalArgumentException("Directory should not be null.");

  }if (!directoryUri.exists()) {

    throw new FileNotFoundException("Directory does not exist: "+ directoryUri);

  }if (!directoryUri.isDirectory()) {

   throw new IllegalArgumentException("Is not a directory: "+ directoryUri);

  }if (!directoryUri.canRead()) {

   throw new IllegalArgumentException("Directory cannot be read: "

                       + directoryUri);

  }

 }

 

        public static void main(String[] args) {

  try {

   System.out.println(getFileUris(new File("c:\\intel")));

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  }

 }

}



Output>>
Let directory uri is: C:\Intel

Result:

[c:\intel\ExtremeGraphics,
c:\intel\ExtremeGraphics\CUI,
c:\intel\ExtremeGraphics\CUI\Resource,
c:\intel\Logs,
c:\intel\Logs\IntelChipset.log,
c:\intel\Logs\IntelGFX.log,
c:\intel\Logs\IntelGFXCoin.log]


No comments:

Post a Comment

Thanks for your comments/Suggestions.