/** * Recursively removes a file or a directory and all its subdirectories. * @param file The file or directory to delete. * @return A value indicating whether the file directory was successfully * removed. If unsuccessful, the directory may have been partially * cleared. */ public static boolean deleteRecursive(File file) { return clearDirectory(file) && file.delete(); }
/** * Writes the specified byte array to a file. * @param file The <code>File</code> to write. * @param contents The byte array to write to the file. * @throws IOException If the file could not be written. */ public static void setFileContents(File file, ByteBuffer contents) throws IOException { setFileContents(file, contents, false); }
/** * Walks a directory tree using pre-order traversal. The contents of a * directory are visited after the directory itself is visited. * @param root The <code>File</code> indicating the file or directory to * walk. * @param visitor The <code>FileVisitor</code> to use to visit files and * directories while walking the tree. * @return A value indicating whether the tree walk was completed without * {@link FileVisitor#visit(File)} ever returning false. * @throws Exception If {@link FileVisitor#visit(File)} threw an exception. * @see FileVisitor#visit(File) */ public static boolean preOrderTraversal(File root, FileVisitor visitor) throws Exception { if (!visitor.visit(root)) { return false; } if (root.isDirectory()) { for (File child : root.listFiles()) { if (!preOrderTraversal(child, visitor)) { return false; } } } return true; }
/** * Recursively removes all entries from a directory. * @param directory The directory to clear. * @return A value indicating whether the directory was successfully * cleared. If unsuccessful, the directory may have been partially * cleared. */ public static boolean clearDirectory(File directory) { if (directory.isDirectory()) { for (File file : directory.listFiles()) { if (!deleteRecursive(file)) { return false; } } } return true; }
public boolean visit(File file) throws IOException { if (file.isFile()) { String name = getRelativePath(file, contents); zs.putNextEntry(new ZipEntry(name)); FileInputStream fs = new FileInputStream(file); StreamUtil.writeStream(fs, zs); fs.close(); zs.closeEntry(); } return true; }
/** * Performs initialization for the currently running JDCP * application. */ public static void initialize() { String homeDirName = System.getProperty("jdcp.home"); File homeDir; if (homeDirName != null) { homeDir = new File(homeDirName); } else { homeDir = FileUtil.getApplicationDataDirectory("jdcp"); System.setProperty("jdcp.home", homeDir.getPath()); } homeDir.mkdir(); if (System.getProperty("derby.system.home") == null) { System.setProperty("derby.system.home", homeDir.getPath()); } }
/** * Gets the <code>String</code> representing the path to a file relative to * a given directory. * @param file The <code>File</code> for which to obtain the relative path. * @param base The <code>File</code> representing the directory that the * resulting path should be relative to. * @return The <code>String</code> representing the relative path. * @throws IOException If a directory along the walk from <code>base</code> * to <code>file</code> could not be read. */ public static String getRelativePath(File file, File base) throws IOException { StringWriter path = new StringWriter(); while (!isAncestor(file, base)) { path.append("../"); } String fileName = file.getAbsolutePath(); String baseName = base.getAbsolutePath(); int prefixLength = baseName.length(); if (!baseName.endsWith("/")) { prefixLength++; } path.append(fileName.substring(prefixLength)); return path.toString(); }
/** * Walks a directory tree using post-order traversal. The contents of a * directory are visited before the directory itself is visited. * @param root The <code>File</code> indicating the file or directory to * walk. * @param visitor The <code>FileVisitor</code> to use to visit files and * directories while walking the tree. * @return A value indicating whether the tree walk was completed without * {@link FileVisitor#visit(File)} ever returning false. * @throws Exception If {@link FileVisitor#visit(File)} threw an exception. * @see FileVisitor#visit(File) */ public static boolean postOrderTraversal(File root, FileVisitor visitor) throws Exception { if (root.isDirectory()) { for (File child : root.listFiles()) { if (!postOrderTraversal(child, visitor)) { return false; } } } return visitor.visit(root); }
preOrderTraversal(contents, new FileVisitor() {
/** * Writes the specified byte array to a file. * @param file The <code>File</code> to write. * @param contents The byte array to write to the file. * @throws IOException If the file could not be written. */ public static void setFileContents(File file, byte[] contents) throws IOException { setFileContents(file, contents, false); }