 |
|
|
ShowClasses.java
|
/*
* Author: Havard Rast Blok
* E-mail: 
* Web : www.rememberjava.com
*/
import java.io.File;
import java.util.*;
import com.rememberjava.io.SimpleFileFilter;
/**
* Show all the class-files refered to by the current
* CLASSPATH
*/
public class ShowClasses
{
SimpleFileFilter fileFilter;
public ShowClasses()
{
String paths[];
//make a file filter witch accpets class files
fileFilter = new SimpleFileFilter( "class" );
//get the paths of the CLASSPATH variable
paths = getPaths();
//show the class files
for(int i = 0; i < paths.length; i++)
{
showClasses( new File(paths[i]) );
}
}
/**
* Returns the directories of the CLASSPATH as elements of a
* String array.
*
* @return array containing the directories
*/
private String[] getPaths()
{
String cp;
StringTokenizer st;
int tokens;
int counter;
String ans[];
//get classpath
cp = System.getProperty("java.class.path");
System.out.println("CLASSPATH=" + cp + "\n");
//applay StringTokenizer and use the underlying OS' pathSeparator (; or :)
st = new StringTokenizer( cp, File.pathSeparator );
tokens = st.countTokens();
ans = new String[tokens];
counter = 0;
//put each path in an array element
while (st.hasMoreTokens())
{
ans[counter] = st.nextToken();
counter++;
}
return ans;
}
/**
* Gets the sub directories of the input directory.
*
* @param path the directory to retreive sub directories from.
* @return array of sub directories. The array is of type Object[],
* however each element of the array may be casted to a File object.
*/
private Object[] getDirectories( File path )
{
Vector tmpVec;
File fileList[];
Object ans[];
tmpVec = new Vector();
//get all the files in the current directory
fileList = path.listFiles();
//add the directories in the list to the vector
for(int i = 0; i < fileList.length; i++)
{
if( fileList[i].isDirectory() )
{
tmpVec.add( fileList[i] );
}
}
//convert the Vector to an array
ans = tmpVec.toArray();
return ans;
}
/**
* Shows the classes of the input directory and its sub directories.
* If the directory does not exist or may not be read, a text message
* will say so.
* @param pathName directory to search for class files
*/
private void showClasses( File pathName )
{
File classes[];
Object directories[];
//Get file names with extension .class in the current directory
classes = pathName.listFiles( fileFilter );
//check if pathName is a directory and readable
if( classes != null )
{
//show the classes in the current directory
for(int i = 0; i < classes.length; i++)
{
System.out.println("" + classes[i]);
}
//get the sub directories of the current directory
directories = getDirectories( pathName );
//show the class files in the sub directories
//RECURSIVE CALL TO THIS METHOD
for(int i = 0; i < directories.length; i++)
{
showClasses( (File)directories[i] );
}
}
else
{
System.out.println( "\n" + pathName + "\n was not found, or error reading.\n");
}
}
public static void main( String [] arg)
{
new ShowClasses();
}
}
|
|
|
 |