Mega Code Archive

 
Categories / Java / File Input Output
 

Directory Walker

/**  * Title:        NoUnit - Identify Classes that are not being unit Tested  *  * Copyright (C) 2001  Paul Browne , FirstPartners.net  *  *  * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License  * as published by the Free Software Foundation; either version 2  * of the License, or (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  *  * @author Paul Browne  * @version 0.6  */ import java.io.*; import java.util.*; /**  * 'Walks' the directory structure and returns files found (including those in sub folders)  */ public class DirectoryWalker {     /**      * Get all the files in the starting directory (and sub dirs)      * No filter      * @param startingDirectory      * @return filesFound (as HashSet)      * @exception java.io.IOException      */     public static HashSet getFiles(String startingDirectory)         throws java.io.IOException {                 return getFiles(startingDirectory,null);     }       /**      * Get all the files in the starting directory (but NOT sub dirs)      * returns only files ending this the filename      * @param startingDirectory      * @return filesFound (as HashSet)      * @exception java.io.IOException      */     public static HashSet getFiles(String startingDirectory,String endFileName)         throws java.io.IOException{                  //Local Variables         String tmpFullFile;         File startDir = new File(startingDirectory);         File tmpFile;         String[] thisDirContents;         HashSet filesFound = new HashSet();                  //Check that this is a valid directory         if (!startDir.isDirectory()) {             throw new java.io.IOException(startingDirectory+" was not a valid directory");         }                  //Get the contents of the current directory         thisDirContents = startDir.list();                      if (thisDirContents!=null) {             //Now loop through , apply filter , or adding them to list of sub dirs             for (int a=0; a<thisDirContents.length;a++) {               //Get Handle to (full) file (inc path)                 tmpFullFile=FileUtil.combineFileAndDirectory(thisDirContents[a],                                                                      startingDirectory);               tmpFile = new File(tmpFullFile);                 //Add to Output if file                 if (tmpFile.isFile()) {                     //Add if file                      if ((endFileName==null)||                         (tmpFullFile.endsWith(endFileName))){                               filesFound.add(tmpFullFile);                     }                 }             }          }                      return filesFound;              }          /**      * Get all the Directories in the starting directory (and sub dirs)      * returns only files ending this the filename      * @param startingDirectory      * @return filesFound (as HashSet)      * @exception java.io.IOException      */     public static HashSet getDirs(String startingDirectory)         throws java.io.IOException{                  //Local Variables         String tmpFullFile;         String tmpSubDir;         File startDir = new File(startingDirectory);         File tmpFile;         String[] thisDirContents;         HashSet dirsFound = new HashSet();         HashSet subDirFilesFound;                  //Check that this is a valid directory         if (!startDir.isDirectory()) {             throw new java.io.IOException(startingDirectory+" was not a valid directory");         }                  //Add the current directory to the output list         dirsFound.add(startingDirectory);                  //Get the contents of the current directory         thisDirContents = startDir.list();                      if (thisDirContents!=null) {             //Now loop through , apply filter , or adding them to list of sub dirs             for (int a=0; a<thisDirContents.length;a++) {               //Get Handle to (full) file (inc path)                 tmpFullFile=FileUtil.combineFileAndDirectory(thisDirContents[a],                                                                      startingDirectory);               tmpFile = new File(tmpFullFile);                                //We're only interested in directories                 if (tmpFile.isDirectory()) {                                          //Add this to the directory list                     dirsFound.add(tmpFullFile);                                          //Now Do Recursive Call (to this method)if Directory                     tmpSubDir = FileUtil.combineFileAndDirectory(thisDirContents[a],                                                                      startingDirectory);                     subDirFilesFound = DirectoryWalker.getDirs(tmpSubDir);                     dirsFound.addAll(subDirFilesFound);                 }                }          }                      return dirsFound;              } } /**  * Title:        NoUnit - Identify Classes that are not being unit Tested  *  * Copyright (C) 2001  Paul Browne , FirstPartners.net  *  *  * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License  * as published by the Free Software Foundation; either version 2  * of the License, or (at your option) any later version.  *  * This program is distributed in the hope that it will be useful,  * but WITHOUT ANY WARRANTY; without even the implied warranty of  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  * GNU General Public License for more details.  *  * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  *  * @author Paul Browne  * @version 0.6  */ /**  * Class to manage files within a directory structure<BR>  */  class FileUtil {  /**    * Combines file and directory , using path delimeters    * @param fileName    * @param filePath    * @return combinedPath    */   public static String combineFileAndDirectory (String fileName , String filePath) {     String combinedPath = null;     //Checking incoming values       if ((fileName==null)||(filePath==null)) {         return null;       }       if ((fileName.equals(""))||(filePath.equals(""))) {         return null;       }     //Add character to end of Directory if required     if ((filePath.endsWith("\\")||(filePath.endsWith("/")))) {       //nothing     } else {       //add character       filePath = filePath + String.valueOf(File.separatorChar);     }     //Add two together     combinedPath = filePath+fileName;     return combinedPath;   }     }