1 package net.firstpartners.nounit.utility; 2 3 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.HashSet ; 30 31 35 public class DirectoryWalker { 36 37 44 public static HashSet getFiles( String startDir ) throws IOException { 45 return getFiles( startDir, null ); 46 } 47 48 55 public static HashSet getFiles( String startingDirectory, 56 String mask ) 57 throws java.io.IOException { 58 59 HashSet filesFound = new HashSet (); 60 61 File startDir = new File ( startingDirectory ); 63 if (!startDir.isDirectory()) { 64 String msg = startingDirectory + " was not a valid directory"; 65 throw new java.io.IOException ( msg ); 66 } 67 68 String [] thisDirContents = startDir.list(); 70 71 if (thisDirContents!=null) { 72 73 for (int a=0; a<thisDirContents.length;a++) { 76 77 File tmp = new File ( startingDirectory, thisDirContents[a] ); 78 79 if ( tmp.isFile() ) { 81 if ( ( null==mask )|| ( tmp.toString().endsWith(mask) ) ) { 82 filesFound.add( tmp.toString() ); 83 } 84 } 85 } 86 } 87 return filesFound; 88 } 89 90 97 public static HashSet getDirs(String startingDirectory) 98 throws java.io.IOException { 99 100 File startDir = new File (startingDirectory); 102 if (!startDir.isDirectory()) { 103 throw new java.io.IOException (startingDirectory+" was not a valid directory"); 104 } 105 106 HashSet dirsFound = new HashSet (); 108 dirsFound.add(startingDirectory); 109 110 String [] thisDirContents = startDir.list(); 112 113 if (thisDirContents!=null) { 114 for (int a=0; a<thisDirContents.length;a++) { 116 117 File tmpFile = 119 new File ( startingDirectory, thisDirContents[a] ); 120 121 if (tmpFile.isDirectory()) { 123 124 dirsFound.add( tmpFile.toString() ); 126 127 File tmpSubDir = new File ( startingDirectory, 129 thisDirContents[a] ); 130 HashSet subDirFilesFound = 131 DirectoryWalker.getDirs( tmpSubDir.toString() ); 132 dirsFound.addAll(subDirFilesFound); 133 } 134 } 135 } 136 return dirsFound; 137 } 138 } 139
| Popular Tags
|