Java Utililty Methods File Delete nio

List of utility methods to do File Delete nio

Description

The list of methods to do File Delete nio are organized into topic(s).

Method

voiddeleteAll(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();
voiddeleteAll(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());
voiddeleteEntriesFromZip(File zipFile, List entriesToDelete)
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 {
...
voiddeleteFile(File f)
Helper function used to delete a file.
deleteFile(f, false);
voiddeleteFile(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());
booleandeleteFile(String fileName)
Deletes the given file
Path p = Paths.get(fileName);
try {
    return Files.deleteIfExists(p);
} catch (IOException e) {
    e.printStackTrace();
return false;
voiddeleteFile(String filename)
delete File
try {
    Path path = FileSystems.getDefault().getPath(filename);
    Files.delete(path);
} catch (IOException e) {
    e.printStackTrace();
voiddeleteFileCascade(String directory)
Delete a folder with all the sub files.
deleteFileCascade(new File(directory));
booleandeleteFileRescursive(File file)
delete File Rescursive
return deleteFileRescursive(file, true);
voiddeleteFiles(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);