Java Delete Tree deltree(String dir)

Here you can find the source of deltree(String dir)

Description

Deletes the directory and its content.

License

Open Source License

Parameter

Parameter Description
dir a parameter

Declaration

public static void deltree(String dir) 

Method Source Code

//package com.java2s;
/*/*from  w  w w .j  a va2  s.co  m*/
 * Copyright (c) 2004-2016 Universidade do Porto - Faculdade de Engenharia
 * Laborat?rio de Sistemas e Tecnologia Subaqu?tica (LSTS)
 * All rights reserved.
 * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
 *
 * This file is part of Neptus, Command and Control Framework.
 *
 * Commercial Licence Usage
 * Licencees holding valid commercial Neptus licences may use this file
 * in accordance with the commercial licence agreement provided with the
 * Software or, alternatively, in accordance with the terms contained in a
 * written agreement between you and Universidade do Porto. For licensing
 * terms, conditions, and further information contact lsts@fe.up.pt.
 *
 * European Union Public Licence - EUPL v.1.1 Usage
 * Alternatively, this file may be used under the terms of the EUPL,
 * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
 * included in the packaging of this file. You may not use this work
 * except in compliance with the Licence. Unless required by applicable
 * law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the Licence for the specific
 * language governing permissions and limitations at
 * http://ec.europa.eu/idabc/eupl.html.
 *
 * For more information please see <http://lsts.fe.up.pt/neptus>.
 *
 * Author: Paulo Dias
 * 2005/01/14
 */

import java.io.File;

public class Main {
    /**
     * Deletes the directory and its content. If dir is a file only deletes the file. If the directory does not exist,
     * nothing will be done.
     * 
     * @param dir
     */
    public static void deltree(String dir) {
        String sep = System.getProperty("file.separator");
        File directory = new File(dir);
        if (!directory.exists())
            return;
        if (!directory.isDirectory()) {
            directory.delete();
        }
        String[] files = directory.list();
        if (files == null)
            return;

        for (String f : files) {
            File tmp = new File(directory.getAbsolutePath() + sep + f);
            if (tmp.isDirectory()) {
                deltree(tmp.getAbsolutePath());
            } else {
                try {
                    tmp.delete();
                } catch (SecurityException e) {
                    e.printStackTrace();
                }
            }
        }
        directory.delete();
    }
}

Related

  1. delTree(File file)
  2. delTree(File root)
  3. deltree(File root)
  4. deltree(final String directory)
  5. deltree(String dir)
  6. deltree(String directory)
  7. delTree(String dirName, String excludedDirName)