/** * Deletes a file or a directory and all of its contents * * @param file * or directory to delete * @return the list of files that could not be deleted */ public static List<File> delete(File file) { List<File> failures = new ArrayList<File>(); if (file.isDirectory()) { deleteContents(file, failures); } if (!file.delete()) { failures.add(file); } return failures; }
@Override public void delete(Book dead) throws BookException { SwordBookMetaData sbmd = (SwordBookMetaData) dead.getBookMetaData(); File confFile = sbmd.getConfigFile(); // We can only uninstall what we download into our download dir. if (confFile == null || !confFile.exists()) { // TRANSLATOR: Common error condition: The file could not be deleted. There can be many reasons. // {0} is a placeholder for the file. throw new BookException(JSMsg.gettext("Unable to delete: {0}", confFile)); } // Delete the conf List<File> failures = FileUtil.delete(confFile); if (failures.isEmpty()) { URI loc = sbmd.getLocation(); if (loc != null) { File bookDir = new File(loc.getPath()); failures = FileUtil.delete(bookDir); Books.installed().removeBook(dead); } } // TODO(DM): list all that failed if (!failures.isEmpty()) { // TRANSLATOR: Common error condition: The file could not be deleted. There can be many reasons. // {0} is a placeholder for the file. throw new BookException(JSMsg.gettext("Unable to delete: {0}", failures.get(0))); } }
/** * Recursive delete files. * * @param dirPath * directory of files to delete * @param failures * the list of files that could not be deleted */ private static void deleteContents(File dirPath, List<File> failures) { String[] ls = dirPath.list(); for (int idx = 0; idx < ls.length; idx++) { File file = new File(dirPath, ls[idx]); if (file.isDirectory()) { deleteContents(file, failures); } if (!file.delete()) { failures.add(file); } } }