001 /* 002 * Copyright (C) 2003 Adam Olsen 003 * This program is free software; you can redistribute it and/or modify 004 * it under the terms of the GNU General Public License as published by 005 * the Free Software Foundation; either version 1, or (at your option) 006 * any later version. 007 * This program is distributed in the hope that it will be useful, 008 * but WITHOUT ANY WARRANTY; without even the implied warranty of 009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 010 * GNU General Public License for more details. 011 * You should have received a copy of the GNU General Public License 012 * along with this program; if not, write to the Free Software 013 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 014 */ 015 package com.valhalla.misc; 016 017 import java.io.*; 018 019 /** 020 * Miscellaneous tools good for any application 021 * 022 * @author synic 023 * @created November 30, 2004 024 */ 025 public class MiscUtils 026 { 027 /** 028 * Deletes a directory, and all the files in it 029 * 030 * @param dir the directory to delete 031 * @exception Exception thrown if there is an error deleting the dir 032 */ 033 public static void recursivelyDeleteDirectory( String dir ) 034 throws Exception 035 { 036 File file = new File( dir ); 037 if( !file.isDirectory() || !file.exists() ) 038 { 039 throw new Exception( dir + " was not a directory, could not recursively delete it" ); 040 } 041 042 File[] files = file.listFiles(); 043 for( int i = 0; i < files.length; i++ ) 044 { 045 if( files[i].isDirectory() ) 046 { 047 recursivelyDeleteDirectory( files[i].getPath() ); 048 } 049 else 050 { 051 if( !files[i].delete() ) 052 { 053 throw new Exception( "Could not delete " + files[i] + "." ); 054 } 055 } 056 } 057 058 if( !file.delete() ) 059 { 060 throw new Exception( "Could not delete " + file + "." ); 061 } 062 } 063 } 064