1 import java.io.*; 2 import java.util.Arrays ; 3 4 11 12 15 public class DeepClassFileRemove 16 { 17 int numRemoved = 0; 18 19 public void start ( String dir ) { start( new String [] { dir } ); } 20 21 public void start ( String [] args ) { 22 if ( args == null || args.length < 1) return; 23 String dirPath = args[0]; 24 dirPath = dirPath.replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ); 25 File p = null; 26 try { 27 numRemoved = 0; 28 p = new File( dirPath ).getCanonicalFile(); 29 if ( p.isDirectory() && p.canRead() ) { 30 delClassFiles( p); 31 System.out.println(" "+numRemoved+" files removed in " + p); 32 } 33 else 34 throw new IOException( "'" + dirPath + "' does not point to a readable directory." ); 35 } 36 catch ( IOException e) { 37 System.err.println( e.getMessage() ); 38 System.exit( 2); 39 } 40 } 41 42 private void delClassFiles( File p) throws IOException { 43 44 File[] topElements = p.listFiles(); 45 Arrays.sort( topElements ); 46 47 for ( int i = 0; i < topElements.length; i++ ) { 48 File f = topElements[i].getCanonicalFile(); 49 if (f.isDirectory() && f.canRead()) 50 delClassFiles( f ); 51 else 52 if (f.isFile() && f.getName().endsWith(".class")) { f.delete(); numRemoved++; } 53 } 54 } 55 56 57 59 public static void main( String [] args) { new DeepClassFileRemove().start( args); } 60 61 } 62 | Popular Tags |