Java File Path Delete deleteDirectory(File path)

Here you can find the source of deleteDirectory(File path)

Description

delete Directory

License

Open Source License

Declaration

static public boolean deleteDirectory(File path) 

Method Source Code

//package com.java2s;
/*/*  w w w.  j  a v a  2  s. c o m*/
 * JimBot - Java IM Bot
 * Copyright (C) 2006-2010 JimBot project
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

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

public class Main {

    static public boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }

    public static Vector<String> listFiles(String dir, String ext) {
        Vector<String> v = new Vector<String>();
        try {
            File f = new File(dir);
            File[] fs = f.listFiles();
            if (fs.length < 0)
                return v;
            for (int i = 0; i < fs.length; i++) {
                if (fs[i].isFile())
                    if (getExt(fs[i].getName()).equals(ext))
                        v.add(fs[i].getName());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return v;
    }

    public static String getExt(String s) {
        if (s.indexOf(".") < 0)
            return "";
        else
            return s.replace('.', ':').split(":")[1];
    }

    public static String getName(String s) {
        if (s.indexOf(".") < 0)
            return s;
        else
            return s.replace('.', ':').split(":")[0];
    }
}

Related

  1. deleteDirectory(File path)
  2. deleteDirectory(File path)
  3. deleteDirectory(File path)
  4. deleteDirectory(File path)
  5. deleteDirectory(File path)
  6. deleteDirectory(File path)
  7. deleteDirectory(File path)
  8. deleteDirectory(File path)
  9. deleteDirectory(File path, boolean deleteMyself, boolean deleteHidden)