/** * A method that will delete multiple file and/or folders. Folders are removed recursively. * * @param filesOrFolder the files and folders to be deleted * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(File... filesOrFolder) throws IOException { for (File fileOrFolder : filesOrFolder) { delete(fileOrFolder); } }
/** * A method that will delete a file or folder only if it is within the 'target' directory (for safety). * Folders are removed recursively. * * @param path the path to the file or folder in the target directory * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(String path) throws IOException { if (path != null) delete(Paths.get(path)); }
/** * A method that will delete a file or folder. Folders are removed recursively. * * @param fileOrFolder the file or folder to be deleted * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(File fileOrFolder) throws IOException { if (fileOrFolder != null) delete(fileOrFolder.toPath()); }
/** * Create a directory at the given absolute or relative path, removing any existing content beforehand. * * @param path the relative or absolute path of the directory to recreate; may not be null * @param removeExistingContent true if any existing content should be removed * @return the reference to the existing readable and writable directory * @throws IOException if there is a problem deleting the files at this path */ public static File createDirectory(Path path, boolean removeExistingContent) throws IOException { File dir = path.toAbsolutePath().toFile(); if (dir.exists() && dir.canRead() && dir.canWrite()) { if (dir.isDirectory()) { delete(path); return dir; } throw new IllegalStateException("Expecting '" + path + "' to be a directory but found a file"); } dir.mkdirs(); return dir; }
/** * Delete all of the data associated with this server. */ public synchronized void deleteData() { if (server == null) { // Delete all data ... try { IoUtil.delete(this.logsDir); } catch (IOException e) { LOGGER.error("Unable to delete directory '{}'", this.logsDir, e); } } }
} finally { try { IoUtil.delete(this.dataDir); } catch (IOException e) { LOGGER.error("Error while deleting cluster data", e);
IoUtil.delete(dataDir); dataDir.mkdirs();
/** * A method that will delete a file or folder only if it is within the 'target' directory (for safety). * Folders are removed recursively. * * @param path the path to the file or folder in the target directory */ public static void delete(Path path) { if (path != null) { path = path.toAbsolutePath(); if (inTestDataDir(path)) { try { IoUtil.delete(path); } catch (IOException e) { printError("Unable to remove '" + path.toAbsolutePath() + "'", e); } } else { printError("Will not remove directory that is outside test data area: " + path); } } }
/** * Shutdown the embedded Kafka server. * * @param deleteData true if the data should be removed, or false otherwise */ public synchronized void shutdown(boolean deleteData) { if (factory != null) { try { factory.shutdown(); try { // Zookeeper 3.4.6 does not close the ZK DB during shutdown, so we must do this here to avoid file locks and open handles... server.getZKDatabase().close(); } catch (IOException e) { LOGGER.error("Unable to close zookeeper DB", e); } } finally { factory = null; if (deleteData) { // Delete all data ... try { IoUtil.delete(this.snapshotDir,this.logDir); } catch ( IOException e ) { LOGGER.error("Unable to delete data upon shutdown",e); } } } } }
/** * A method that will delete multiple file and/or folders. Folders are removed recursively. * * @param filesOrFolder the files and folders to be deleted * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(File... filesOrFolder) throws IOException { for (File fileOrFolder : filesOrFolder) { delete(fileOrFolder); } }
/** * A method that will delete a file or folder only if it is within the 'target' directory (for safety). * Folders are removed recursively. * * @param path the path to the file or folder in the target directory * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(String path) throws IOException { if (path != null) delete(Paths.get(path)); }
/** * A method that will delete a file or folder. Folders are removed recursively. * * @param fileOrFolder the file or folder to be deleted * @throws IOException if there is a problem deleting the file at the given path */ public static void delete(File fileOrFolder) throws IOException { if (fileOrFolder != null) delete(fileOrFolder.toPath()); }
/** * Create a directory at the given absolute or relative path, removing any existing content beforehand. * * @param path the relative or absolute path of the directory to recreate; may not be null * @param removeExistingContent true if any existing content should be removed * @return the reference to the existing readable and writable directory * @throws IOException if there is a problem deleting the files at this path */ public static File createDirectory(Path path, boolean removeExistingContent) throws IOException { File dir = path.toAbsolutePath().toFile(); if (dir.exists() && dir.canRead() && dir.canWrite()) { if (dir.isDirectory()) { delete(path); return dir; } throw new IllegalStateException("Expecting '" + path + "' to be a directory but found a file"); } dir.mkdirs(); return dir; }