Java Delete Directory deleteDirectory(File dir, Map preserve)

Here you can find the source of deleteDirectory(File dir, Map preserve)

Description

Recursively deletes a directory

License

Open Source License

Parameter

Parameter Description
dir the File object for the directory
preserve a Map with String keys indicating elements within the target directory to keep (that is, not to delete); this parameter can be null if none are to be preserved.

Return

true if the directory was removed, false if it was not (because at least some of its contents were preserved).

Declaration

public static boolean deleteDirectory(File dir, Map<String, ?> preserve) 

Method Source Code


//package com.java2s;
/*==========================================================================*\
 |  $Id: FileUtilities.java,v 1.9 2011/05/27 15:30:56 stedwar2 Exp $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2006-2008 Virginia Tech
 |
 |  This file is part of Web-CAT./*from w  w w. j  a v  a2s . c  o m*/
 |
 |  Web-CAT is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU Affero General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  Web-CAT is distributed in the hope that it will be useful,
 |  but WITHOUT ANY WARRANTY; without even the implied warranty of
 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 |  GNU General Public License for more details.
 |
 |  You should have received a copy of the GNU Affero General Public License
 |  along with Web-CAT; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

import java.io.*;
import java.util.*;

public class Main {
    /**
     * Recursively deletes a directory
     *
     * @param dir the File object for the directory
     * @param preserve a Map with String keys indicating elements within
     *        the target directory to keep (that is, not to delete); this
     *        parameter can be null if none are to be preserved.
     * @return true if the directory was removed, false if it was not
     *  (because at least some of its contents were preserved).
     */
    public static boolean deleteDirectory(File dir, Map<String, ?> preserve) {
        if (dir == null || !dir.exists()) {
            return true;
        }

        File[] files = dir.listFiles();
        boolean deletedAll = true;
        for (int i = 0; i < files.length; i++) {
            if (preserve != null) {
                // This is just a linear search, because the preserve
                // lists are so short in general that it is not worth the
                // effort to speed them up.
                if (preserve.containsKey(normalizeFileName(files[i]))) {
                    deletedAll = false;
                    continue;
                }
            }

            if (files[i].isDirectory()) {
                deletedAll = deleteDirectory(files[i], preserve) && deletedAll;
            }
            files[i].delete();
        }
        if (deletedAll) {
            dir.delete();
        }
        return deletedAll;
    }

    /**
     * Return a canonical version of the file name, using "/" as the path
     * seperator instead of "\".
     *
     * @param name the File with the name to convert
     * @return the canonical version of the file's name
     */
    public static String normalizeFileName(File name) {
        try {
            return name.getCanonicalPath().replace('\\', '/');
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Return a canonical version of the file name, using "/" as the path
     * seperator instead of "\".
     *
     * @param name the name to convert
     * @return the canonical version of the file's name
     */
    public static String normalizeFileName(String name) {
        return normalizeFileName(new File(name));
    }
}

Related

  1. deleteDirectory(File dir)
  2. deleteDirectory(File dir)
  3. deleteDirectory(File dir, boolean isInitialDelete)
  4. deleteDirectory(File dir, boolean isInitialDelete)
  5. deleteDirectory(File dir, boolean recursive)
  6. deleteDirectory(File directory)
  7. deleteDirectory(File directory)
  8. deleteDirectory(File directory)
  9. deleteDirectory(File directory)