List of utility methods to do File Delete nio
void | deleteAll(File f) delete All Path start = FileSystems.getDefault().getPath(f.getAbsolutePath()); try { Files.walkFileTree(start, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { throw e; }); } catch (IOException e) { e.printStackTrace(); |
void | deleteAll(File file) Deletes all files and directory specified recursively including passed file itself. if (file == null || !file.exists()) { return; if (file.isDirectory()) { for (File f : file.listFiles()) { deleteAll(f); if (!file.isDirectory()) { if (!file.setWritable(true, false)) { throw new IOException(String.format("Failed to change file to writeable [%s].", file)); Files.delete(file.toPath()); |
void | deleteEntriesFromZip(File zipFile, List Use this to delete some entries from a zip file. byte[] buffer = new byte[1024 * 32]; File newZipFile = new File(zipFile.getParentFile(), zipFile.getName() + ".tmp"); if (newZipFile.exists()) { newZipFile.delete(); ZipOutputStream zos = null; ZipInputStream zis = null; try { ... |
void | deleteFile(File f) Helper function used to delete a file. deleteFile(f, false); |
void | deleteFile(File f) delete File if (!f.exists()) return; boolean canUseNio = true; try { Class.forName("java.nio.file.Path"); } catch (ClassNotFoundException e) { canUseNio = false; if (canUseNio) { java.nio.file.Path fp = f.toPath(); java.nio.file.Files.delete(fp); } else if (!f.delete()) throw new IOException("Failed to delete file: " + f.getName()); |
boolean | deleteFile(String fileName) Deletes the given file Path p = Paths.get(fileName); try { return Files.deleteIfExists(p); } catch (IOException e) { e.printStackTrace(); return false; |
void | deleteFile(String filename) delete File try { Path path = FileSystems.getDefault().getPath(filename); Files.delete(path); } catch (IOException e) { e.printStackTrace(); |
void | deleteFileCascade(String directory) Delete a folder with all the sub files. deleteFileCascade(new File(directory));
|
boolean | deleteFileRescursive(File file) delete File Rescursive return deleteFileRescursive(file, true);
|
void | deleteFiles(File directory, String affix) Delete all the files and sub directories which matches given prefix in a given directory. if (!directory.isDirectory()) { return; for (File f : directory.listFiles()) { if (f.getName().startsWith(affix) || f.getName().endsWith(affix)) { deleteDirectory(f); |