Java examples for java.io:Path Name
Remove a directory and all of its contents
/**//from www. j a v a 2s . c o m * e-Science Central * Copyright (C) 2008-2013 School of Computing Science, Newcastle University * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation at: * http://www.gnu.org/licenses/gpl-2.0.html * * 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, 5th Floor, Boston, MA 02110-1301, USA. */ //package com.java2s; import java.io.*; public class Main { /** Remove a directory and all of its contents */ public static void removeDirectory(File dir) throws IOException { if (dir.isDirectory()) { File[] contents = dir.listFiles(); for (int i = 0; i < contents.length; i++) { if (contents[i].isFile()) { contents[i].delete(); } else { removeDirectory(contents[i]); } } // Remove this dirctory dir.delete(); } else { throw new IOException("File: " + dir.getName() + " is not a directory"); } } }