Java Recursive Delete recursivelyDelete(File dir)

Here you can find the source of recursivelyDelete(File dir)

Description

recursively Delete

License

Apache License

Declaration

private static void recursivelyDelete(File dir) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.File;
import java.io.IOException;

public class Main {
    private static void recursivelyDelete(File dir) throws IOException {
        if (!dir.getPath().equals(dir.getCanonicalPath())) {
            // Directory symlink reaching outside of temporary space.
            return;
        }/*from  w ww .ja va 2 s  .com*/
        File[] contents = dir.listFiles();
        if (contents != null) {
            for (File d : contents) {
                if (d.isDirectory()) {
                    recursivelyDelete(d);
                } else {
                    deleteNowOrOnExit(d);
                }
            }
        }
        deleteNowOrOnExit(dir);
    }

    private static void deleteNowOrOnExit(File dir) {
        if (!dir.delete()) {
            dir.deleteOnExit();
        }
    }
}

Related

  1. recursiveDeleteNoCheck(File f)
  2. recursiveDeleteOnExit(File parent)
  3. recursiveDeleteOnExit(File rootDir)
  4. recursiveDeleteOnExitHelper(File fileOrDir)
  5. recursivelyDelete(File aDirectory)
  6. recursivelyDelete(final File root, final boolean deleteRoot)
  7. recursivelyDelete(String loc)
  8. recursivelyDeleteEmptyDirectories(File fileToDelete)
  9. recursivelyDeleteEmptyParentDirectoriesUpToRoot(String path, String root)