Utils { public static void deleteFilesRecursively( File... files ) throws IOException { if( files == null ) return; List<File> filesToDelete = new ArrayList<> (); filesToDelete.addAll( Arrays.asList( files )); while( ! filesToDelete.isEmpty()) { File currentFile = filesToDelete.remove( 0 ); if( currentFile == null || ! currentFile.exists()) continue; File[] subFiles = currentFile.listFiles(); if( subFiles != null && subFiles.length > 0 ) { filesToDelete.add( 0, currentFile ); filesToDelete.addAll( 0, Arrays.asList( subFiles )); } else if( ! currentFile.delete()) throw new IOException( currentFile.getAbsolutePath() + " could not be deleted." ); } } private  Utils(); static boolean isEmptyOrWhitespaces( String s ); static String capitalize( String s ); static String removeFileExtension( String filename ); static List<String> splitNicely( String toSplit, String separator ); static List<String> splitNicelyWithPattern( String toSplit, String patternSeparator ); static List<String> filterEmptyValues( List<String> values ); static String format( Collection<String> items, String separator ); static String expandTemplate(String s, Properties params); static void closeQuietly( InputStream in ); static void closeQuietly( OutputStream out ); static void closeQuietly( Reader reader ); static void closeQuietly( Writer writer ); static void copyStreamUnsafelyUseWithCaution( InputStream in, OutputStream os ); static void copyStreamSafely( InputStream in, ByteArrayOutputStream os ); static void copyStream( InputStream in, File outputFile ); static void copyStream( File inputFile, File outputFile ); static void copyStream( File inputFile, OutputStream os ); static void writeStringInto( String s, File outputFile ); static void appendStringInto( String s, File outputFile ); static String readFileContent( File file ); static String readFileContentQuietly( File file, Logger logger ); static String readUrlContent( String url ); static String readUrlContentQuietly( String url, Logger logger ); static Properties readPropertiesFile( File file ); static Properties readPropertiesFileQuietly( File file, Logger logger ); static Properties readPropertiesQuietly( String fileContent, Logger logger ); static void writePropertiesFile( Properties properties, File file ); static String cleanNameWithAccents( String name ); static void createDirectory( File directory ); static List<File> listAllFiles( File directory ); static String updateProperties( String propertiesContent, Map<String,String> keyToNewValue ); static List<File> listAllFiles( File directory, String fileExtension ); static List<File> listAllFiles( File directory, boolean includeDirectories ); static List<File> listDirectFiles( File directory, String fileExtension ); static List<File> listDirectories( File root ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory, List<String> exclusionPatteners ); static Map<String,String> storeDirectoryResourcesAsString( File directory ); static String computeFileRelativeLocation( File rootDirectory, File subFile ); static void extractZipArchive( File zipFile, File targetDirectory ); static void extractZipArchive( File zipFile, File targetDirectory, String entryPattern, String removedEntryPrefix ); static boolean isAncestor( File ancestorCandidate, File file ); static void deleteFilesRecursively( File... files ); static void deleteFilesRecursivelyAndQuietly( File... files ); static String writeExceptionButDoNotUseItForLogging( Throwable t ); static void logException( Logger logger, Level logLevel, Throwable t, String message ); static void logException( Logger logger, Level logLevel, Throwable t ); static void logException( Logger logger, Throwable t ); static void closeStatement( PreparedStatement ps, Logger logger ); static void closeStatement( Statement st, Logger logger ); static void closeResultSet( ResultSet resultSet, Logger logger ); static void closeConnection( Connection conn, Logger logger ); static boolean isAncestorFile( File potentialAncestor, File file ); static void copyDirectory( File source, File target ); static Map.Entry<String,Integer> findUrlAndPort( String url ); static String getValue(Map<String,String> map, String key, String defaultValue);  }
@Test public void testDeleteFilesRecursively() { File tmpDir = new File( System.getProperty( "java.io.tmpdir" ), UUID.randomUUID().toString()); if( ! tmpDir.mkdir()) Assert.fail( "Could not create a temporary directory." ); String[] dirs = { "dir1", "dir1/dir2", "dir1/dir2/dir3", "dir1/dir2/dir4", "dir2", "dir1/dir54" }; for( String dir : dirs ) { File f = new File( tmpDir, dir ); if( ! f.mkdir()) Assert.fail( "Could not create a sub-directory: " + dir ); } String[] files = { "test.txt", "te.txt", "dir1/test.txt", "dir2/some.txt", "dir1/dir2/dir3/pol.txt" }; try { for( String file : files ) { File f = new File( tmpDir, file ); if( ! f.createNewFile()) Assert.fail( "Could not create a file: " + file ); } } catch( IOException e ) { Assert.fail( "Could not create a file. " + e.getMessage()); } Assert.assertTrue( tmpDir.exists()); try { Utils.deleteFilesRecursively( tmpDir ); Assert.assertFalse( "Temp directory could not be deleted: " + tmpDir.getName(), tmpDir.exists()); } catch( IOException e ) { Assert.fail( "Failed to delete the temporary directory." ); } try { Utils.deleteFilesRecursively((File) null); } catch( IOException e ) { Assert.fail( "Null file must be supported" ); } try { Utils.deleteFilesRecursively((File[]) null); } catch( IOException e ) { Assert.fail( "Null file array must be supported" ); } try { File[] nullFiles = new File[] { null, null }; Utils.deleteFilesRecursively( nullFiles ); } catch( IOException e ) { Assert.fail( "Array of null files must be supported" ); } try { Utils.deleteFilesRecursively( new File( "inexisting-file" )); } catch( IOException e ) { Assert.fail( "Inexisting files must be supported" ); } Utils.deleteFilesRecursivelyAndQuietly( new File( "another-inexisting-file" )); }