KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DeepClassFileRemove


1 import java.io.*;
2 import java.util.Arrays JavaDoc;
3
4 /**
5  * Minimalistic recursive class file remover.
6  * Used for installation / update.
7  *
8  * @author Ulf Goldammer
9  * @version 1.0
10 */

11
12 // Note that unix VM JDK 1.3.1/Sun, identifies fifos as files and also
13
// dirs with perm 0666 as (unreadable) files.
14

15 public class DeepClassFileRemove
16 {
17     int numRemoved = 0;
18     
19     public void start ( String JavaDoc dir ) { start( new String JavaDoc[] { dir } ); }
20
21     public void start ( String JavaDoc[] args ) {
22         if ( args == null || args.length < 1) return;
23         String JavaDoc 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 ////////////////////////////////////////////////////////////////////////////////////////////////////
58

59     public static void main( String JavaDoc[] args) { new DeepClassFileRemove().start( args); }
60
61 }
62
Popular Tags