Java operations for deleting files and folders
/*** Delete a single file ** @ param sPath * path of the deleted file + file name * @ return true if a single file is deleted successfully, otherwise, false */public static boolean deleteFile (String sPath) {Boolean flag = false; File file = new File (sPath ); // Delete if (file. isFile () & file. exists () {file. delete (); flag = true;} return flag;}/*** delete directory (folder) and the file in the directory ** @ param sPath * is the file path of the deleted directory * @ return: returns true if the directory is deleted successfully; otherwise, returns false */public static boolean deleteDirectory (S Tring sPath) {// if sPath does not end with a file separator, the file separator if (! SPath. endsWith (File. separator) {sPath = sPath + File. separator;} File dirFile = new File (sPath); // exit if (! DirFile. exists () |! DirFile. isDirectory () {return false;} Boolean flag = true; // delete all files in the folder (including subdirectories) File [] files = dirFile. listFiles (); for (int I = 0; I <files. length; I ++) {// Delete the sub-file if (files [I]. isFile () {flag = deleteFile (files [I]. getAbsolutePath (); if (! Flag) break;} // Delete the subdirectory else {flag = deleteDirectory (files [I]. getAbsolutePath (); if (! Flag) break;} if (! Flag) return false; // delete the current directory if (dirFile. delete () {return true;} else {return false ;}}