1 12 13 package com.openedit.util; 14 15 import java.io.File ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.io.Reader ; 21 import java.io.Writer ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 31 public class FileUtils 32 { 33 OutputFiller fieldFiller = new OutputFiller(); 34 private static final Log log = LogFactory.getLog(FileUtils.class); 35 47 public File createTempDir( String inPrefix ) throws IOException 48 { 49 File tempDir = File.createTempFile( inPrefix, null ); 50 tempDir.delete(); 51 tempDir.mkdir(); 52 53 return tempDir; 54 } 55 56 62 public void deleteAll( String fname ) 63 { 64 File f = new File ( fname ); 65 deleteAll( f ); 66 } 67 68 72 public void deleteMatch( String inMatch ) 73 { 74 File search = new File ( inMatch ); 76 File dir = search.getParentFile(); 77 78 File [] all = dir.listFiles(); 79 if ( all != null) 80 { 81 for (int i = 0; i < all.length; i++) 82 { 83 File f = all[i]; 84 if ( PathUtilities.match(f.getName(), search.getName() ) ) 85 { 86 log.info("deleted " + f.getName()); 87 f.delete(); } 89 } 90 } 91 } 92 93 public boolean deleteOlderVersions( String inDir ) 94 { 95 System.gc(); 96 boolean requiresRestart = false; 97 Map map = new HashMap (); 98 File dir = new File ( inDir ); 100 101 File [] all = dir.listFiles(); 102 if ( all != null) 103 { 104 for (int i = 0; i < all.length; i++) 105 { 106 File f = all[i]; 107 String fileName = f.getName(); 108 int dashIndex = fileName.lastIndexOf('-'); 109 if (dashIndex >= 0) 110 { 111 String base = fileName.substring(0, dashIndex); 112 String version = fileName.substring(dashIndex + 1 ); 113 String highestVersion = (String )map.get(base); 114 if (highestVersion == null || highestVersion.compareTo(version) < 0) 115 { 116 map.put(base, version); 117 } 118 } 119 } 120 121 for (int i = 0; i < all.length; i++) 122 { 123 File f = all[i]; 124 String fileName = f.getName(); 125 int dashIndex = fileName.lastIndexOf('-'); 126 if (dashIndex >= 0) 127 { 128 String base = fileName.substring(0, dashIndex); 129 String version = fileName.substring(dashIndex + 1); 130 String highestVersion = (String )map.get(base); 131 if (!version.equals(highestVersion)) 132 { 133 if (f.delete()) 134 { 135 log.info("deleting " + f.getName()); 136 } 137 else 138 { 139 log.info("deleting " + f.getName() + " on exit"); 140 f.deleteOnExit(); 141 requiresRestart = true; 142 } 143 } 144 } 145 } 146 } 147 return requiresRestart; 148 } 149 150 public void deleteAll( File file ) 151 { 152 if (file.isDirectory()) 153 { 154 File [] fileList = file.listFiles(); 156 157 if (fileList != null) 158 { 159 for ( int idx = 0; idx < fileList.length; idx++ ) 160 deleteAll( fileList[idx] ); 161 } 162 } 163 164 file.delete(); 166 } 167 public void copyFileByMatch( String inMatch, String outDir) throws IOException 168 { 169 File out = new File ( outDir ); 170 out.mkdirs(); 171 copyFileByMatch(inMatch, out ); 172 } 173 public void copyFileByMatch( String inMatch, File outDir) throws IOException 174 { 175 File file = new File ( inMatch ); 176 File dir = file.getParentFile(); 177 178 File [] all = dir.listFiles(); 179 if ( all != null) 180 { 181 for (int i = 0; i < all.length; i++) 182 { 183 File f = all[i]; 184 if ( PathUtilities.match(f.getName(), file.getName() ) ) 185 { 186 copyFiles(f, outDir); 187 } 188 } 189 } 190 } 191 192 205 public void dirCopy( File inSource, File inDest ) throws IOException 206 { 207 if (inSource.isDirectory()) 208 { 209 inDest.mkdirs(); 210 211 File [] files = inSource.listFiles(); 212 if( files != null) 213 { 214 for ( int i = 0; i < files.length; i++ ) 215 { 216 File newDest = new File ( inDest, files[i].getName() ); 218 219 if (files[i].isDirectory()) 220 { 221 dirCopy( files[i], newDest ); 222 } 223 else 224 { 225 fileCopy( files[i], newDest ); 226 } 227 } 228 } 229 } 230 } 231 238 public void move( File inSource, File inDest ) throws IOException 239 { 240 if (inSource.isDirectory()) 241 { 242 inDest.mkdirs(); 243 244 File [] files = inSource.listFiles(); 245 if( files != null) 246 { 247 for ( int i = 0; i < files.length; i++ ) 248 { 249 File newDest = new File ( inDest, files[i].getName() ); 251 252 if (files[i].isDirectory()) 253 { 254 move( files[i], newDest ); 255 } 256 else 257 { 258 files[i].renameTo( newDest ); 259 } 260 } 261 } 262 inSource.delete(); 263 } 264 else 265 { 266 inDest.getParentFile().mkdirs(); 267 inSource.renameTo( inDest ); 268 } 269 } 270 271 284 public void fileCopy( File src, File dst ) throws IOException 285 { 286 fieldFiller.fill(src,dst); 287 288 dst.setLastModified( src.lastModified() ); 290 } 291 public void copyFiles( String source, String destination ) throws IOException 292 { 293 copyFiles( new File ( source ), new File ( destination ) ); 294 } 295 public void copyFiles( File source, File destination ) throws IOException 296 { 297 if (source.isDirectory()) 298 { 299 if( destination.exists() && !destination.isDirectory() ) 300 { 301 destination.delete(); 302 } 303 destination.mkdirs(); 304 305 File [] children = source.listFiles(); 307 308 for ( int i = 0; i < children.length; i++ ) 309 { 310 copyFiles( children[i], new File ( destination, children[i].getName() ) ); 311 } 312 } 313 else 314 { 315 if( destination.isDirectory() ) 316 { 317 copyFiles( source,new File ( destination , source.getName() ) ); 318 } 319 else 320 { 321 destination.getParentFile().mkdirs(); 322 fieldFiller.fill( source,destination ); 323 destination.setLastModified(source.lastModified()); 324 } 325 } 326 } 327 333 public void writeFile( InputStream inInput, File inOutputFile ) throws IOException 334 { 335 338 inOutputFile.getParentFile().mkdirs(); 340 OutputStream out = new FileOutputStream ( inOutputFile ); 341 try 342 { 343 fieldFiller.fill( inInput, out ); 344 out.flush(); 345 } 346 finally 347 { 348 if ( inInput != null) 349 { 350 inInput.close(); 351 } 352 out.close(); 353 } 354 } 355 356 359 public static void safeClose(Reader inIn) 360 { 361 if ( inIn != null) 362 { 363 try 364 { 365 inIn.close(); 366 } 367 catch (IOException ex) 368 { 369 log.error(ex); 370 } 371 } 372 } 373 public static void safeClose(InputStream inIn) 374 { 375 if ( inIn != null) 376 { 377 try 378 { 379 inIn.close(); 380 } 381 catch (IOException ex) 382 { 383 log.error(ex); 384 } 385 } 386 } 387 public static void safeClose(OutputStream inIn) 388 { 389 if ( inIn != null) 390 { 391 try 392 { 393 inIn.close(); 394 } 395 catch (IOException ex) 396 { 397 log.error(ex); 398 } 399 } 400 } 401 public static void safeClose(Writer inIn) 402 { 403 if ( inIn != null) 404 { 405 try 406 { 407 inIn.close(); 408 } 409 catch (IOException ex) 410 { 411 log.error(ex); 412 } 413 } 414 } 415 } | Popular Tags |