public static boolean deleteFileOrDir( File filehandle ) throws IOException {

        if (filehandle.isDirectory()) {
            Files.walkFileTree(Paths.get(filehandle.getAbsolutePath()), new SimpleFileVisitor<Path>(){
                @Override
                public FileVisitResult visitFile( Path file, BasicFileAttributes attrs ) throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory( Path dir, IOException exc ) throws IOException {
                    if (exc != null) {
                        throw exc;
                    }
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        }

        boolean isdel = filehandle.delete();
        if (!isdel) {
            // if it didn't work, which somtimes happens on windows systems,
            // remove on exit
            filehandle.deleteOnExit();
        }

        return isdel;
    }
public static boolean deleteFileOrDir ( File filehandle ) throws IOException { if ( filehandle . isDirectory ( ) ) { Files . walkFileTree ( Paths . get ( filehandle . getAbsolutePath ( ) ) , new SimpleFileVisitor < Path > ( ) { @ Override public FileVisitResult visitFile ( Path file , BasicFileAttributes attrs ) throws IOException { Files . delete ( file ) ; return FileVisitResult . CONTINUE ; } @ Override public FileVisitResult postVisitDirectory ( Path dir , IOException exc ) throws IOException { if ( exc != null ) { throw exc ; } Files . delete ( dir ) ; return FileVisitResult . CONTINUE ; } } ) ; } boolean isdel = filehandle . delete ( ) ; if ( ! isdel ) { filehandle . deleteOnExit ( ) ; } return isdel ; }
Returns true if all deletions were successful. If a deletion fails, the method stops attempting to delete and
returns false.

@param filehandle the file or folder to remove.

@return true if all deletions were successful
@throws IOException