Here you can find the source of deleteDirectory(File directory)
http://stackoverflow.com/questions/3775694/deleting-folder-from-java
Parameter | Description |
---|---|
directory | the directory to delete. |
Parameter | Description |
---|---|
IOException | if the file access fails. |
public static boolean deleteDirectory(File directory) throws IOException
//package com.java2s; /** The FileUtils class contains methods for file and directory handling, which comprises reading a file, copying or deleting a directory as well as things like that.//w ww . j av a2 s . c om <pre> This file is part of FidoCadJ. FidoCadJ 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 3 of the License, or (at your option) any later version. FidoCadJ 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 FidoCadJ. If not, see <http://www.gnu.org/licenses/>. Copyright 2008-2015 by Davide Bucci </pre> */ import java.io.*; public class Main { /** http://stackoverflow.com/questions/3775694/deleting-folder-from-java @param directory the directory to delete. @return true if deletion was successful. @throws IOException if the file access fails. */ public static boolean deleteDirectory(File directory) throws IOException { if (directory.exists()) { File[] files = directory.listFiles(); if (null != files) { for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { deleteDirectory(files[i]); } else { if (!files[i].delete()) throw new IOException("Can not delete file" + files[i]); } } } } return directory.delete(); } }