Thursday, May 12, 2016

Get All Files from folder Including from Sub Folders In Java


Code Snippet :

       /**
        * Gets the all files from folder.
        *
        * @param directoryName the directory name
        * @param files the files
        * @return the all files from folder
        */
       public static void getAllFilesFromFolder(String directoryName, ArrayList<File> files) {
         File directory = new File(directoryName);
         File[] fList = directory.listFiles();
         for (File fileInstance : fList) {
           if (fileInstance.isFile() && fileInstance.getName().endsWith(".xhtml")) {
             files.add(fileInstance);
           } else if (fileInstance.isDirectory()) {
             getAllFilesFromFolder(fileInstance.getAbsolutePath(), files);
           }
         }
       }


The above code snippet gets all the files from the folders including the files in the sub folders and then check whether the file name ends with .xhtml and filters the files which are of Extension .xhtml.
Similarly users can filter the required file formats using the required file extensions

No comments:

Post a Comment