1 16 17 package org.apache.jetspeed.util; 18 19 import java.io.File ; 20 21 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 23 import org.apache.jetspeed.services.logging.JetspeedLogger; 24 25 26 33 public class DirectoryUtils 34 { 35 38 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DirectoryUtils.class.getName()); 39 40 public static void main(String [] args) 41 { 42 DirectoryUtils.rmdir(args[0]); 43 } 44 45 52 public static final boolean rmdir(String directory) 53 { 54 try 55 { 56 File dir = new File (directory); 57 if (!dir.isDirectory()) 58 { 59 dir.delete(); 60 return true; 61 } 62 63 deleteTraversal(directory); 64 65 dir.delete(); 66 return true; 67 68 } 69 catch (Exception e) 70 { 71 logger.error("Error in rmdir utility:" , e); 72 return false; 73 } 74 } 75 76 85 private static void deleteTraversal(String path) 86 { 87 File file = new File (path); 88 if (file.isFile()) 89 { 90 try 91 { 92 file.delete(); 93 } 94 catch (Exception e) 95 { 96 logger.error("Failed to Delete file: " + path + " : " , e); 97 file.deleteOnExit(); } 99 } 100 else if (file.isDirectory()) 101 { 102 if (!path.endsWith(File.separator)) 103 path += File.separator; 104 105 String list[] = file.list(); 106 107 for(int ix = 0; list != null && ix < list.length; ix++) 109 deleteTraversal(path + list[ix]); 110 111 try 113 { 114 file.delete(); 115 } 116 catch (Exception e) 117 { 118 logger.error("Failed to Delete directory: " + path + " : " , e); 119 file.deleteOnExit(); } 121 122 } 123 } 124 } 125 126 127 128 | Popular Tags |