public static boolean copyDirectoryWithFileFilter ( final File source , final File destination , final FileFilter includeFileFilter , final FileFilter excludeFileFilter , final Collection < File > excludeFiles , final boolean lastModified ) throws IOException , FileIsNotADirectoryException , FileIsADirectoryException , FileIsSecurityRestrictedException , DirectoryAlreadyExistsException { if ( ! source . isDirectory ( ) ) { throw new FileIsNotADirectoryException ( "Source file '" + source . getAbsolutePath ( ) + "' is not a directory." ) ; } if ( ! destination . exists ( ) ) { CreateFileExtensions . newDirectory ( destination ) ; } boolean copied = false ; File [ ] includeFilesArray ; if ( null != includeFileFilter ) { includeFilesArray = source . listFiles ( includeFileFilter ) ; } else { includeFilesArray = source . listFiles ( ) ; } if ( null != includeFilesArray ) { File [ ] excludeFilesArray ; List < File > allExcludeFilesList = null ; List < File > excludeFileFilterList ; if ( null != excludeFileFilter ) { excludeFilesArray = source . listFiles ( excludeFileFilter ) ; excludeFileFilterList = Arrays . asList ( excludeFilesArray ) ; allExcludeFilesList = new ArrayList <> ( excludeFileFilterList ) ; } if ( excludeFiles != null && ! excludeFiles . isEmpty ( ) ) { if ( allExcludeFilesList != null ) { allExcludeFilesList . addAll ( excludeFiles ) ; } else { allExcludeFilesList = new ArrayList <> ( excludeFiles ) ; } } // if excludeFilesList is not null and not empty if ( null != allExcludeFilesList && ! allExcludeFilesList . isEmpty ( ) ) { for ( final File element : includeFilesArray ) { final File currentFile = element ; // if the excludeFilesList does not contain the current file do copy... if ( ! allExcludeFilesList . contains ( currentFile ) ) { final File copy = new File ( destination , currentFile . getName ( ) ) ; if ( currentFile . isDirectory ( ) ) { // copy directory recursive... copied = copyDirectoryWithFileFilter ( currentFile , copy , includeFileFilter , excludeFileFilter , lastModified ) ; } else { copied = copyFile ( currentFile , copy , lastModified ) ; } } // otherwise do not copy the current file... } } else { // otherwise copy all files and directories for ( final File currentFile : includeFilesArray ) { final File copy = new File ( destination , currentFile . getName ( ) ) ; if ( currentFile . isDirectory ( ) ) { // copy directory recursive... copied = copyDirectoryWithFileFilter ( currentFile , copy , includeFileFilter , excludeFileFilter , lastModified ) ; } else { copied = copyFile ( currentFile , copy , lastModified ) ; } } } } else { throw new FileIsSecurityRestrictedException ( "File '" + source . getAbsolutePath ( ) + "' is security restricted." ) ; } return copied ; }
Copies all files that match to the given includeFileFilter and does not copy all the files that match the excludeFileFilter from the given source directory to the given destination directory with the option to set the lastModified time from the given destination file or directory .