public static void deleteFileOrDirectory ( File file ) throws IOException { if ( file . exists ( ) ) { if ( file . isDirectory ( ) ) { Path rootPath = Paths . get ( file . getAbsolutePath ( ) ) ; Files . walk ( rootPath , FileVisitOption . FOLLOW_LINKS ) . sorted ( Comparator . reverseOrder ( ) ) . map ( Path :: toFile ) . forEach ( File :: delete ) ; } else { file . delete ( ) ; } } else { throw new RuntimeException ( "File or directory does not exist" ) ; } }
Delete a file or directory