dLife Home Page

dlife.sys
Class FileSystemUtilities

java.lang.Object
  extended by dlife.sys.FileSystemUtilities

public final class FileSystemUtilities
extends Object

This class provides some general utility methods for working with files and directories. In particular some methods help in obtaining lists of files and classes from directories or from the dlife package.

Version:
Apr 27, 2007
Author:
Grant Braught, Dickinson College

Constructor Summary
FileSystemUtilities()
           
 
Method Summary
static void copyFile(String f1, String f2)
          Copy the contents of one file to another file.
static void deleteDirectory(String path)
          Delete the specified directory and all of its contents.
static ArrayList<String> getAllDLifeSubClasses(String superClass)
          Get a list the fully qualified names of all of the concrete sub classes of the specified super class within the dlife package.
static String[] getDirectoryListing(String dirPath)
          Get a list of all of the files in the specified directory.
static String getDLifeClassPathEntry()
          Get the entry from the current classpath that corresponds to the dlife package.
static String getDlifeHome(String classPath)
          Given the dLife class path entry this method finds dlife's home directory.
static String getFilename(String fullPath)
          Get the filename from a full path.
static String getPath(String fullPath)
          Get the path from a full path to a file.
static String[] getPathAndName(String fullPath)
          Given an absolute path to a .class file, this method returns an Array of two Strings containing the classpath to the package that holds the class and the fully qualified class name.
static String getPathForJavaSourceFile(String javaSourceFile)
          Determine the path to the specified java source file accounting for the possibility that the java source file contains a package statement.
static String getPathFromBinaryName(String binaryName)
          Given a binary name for a class (e.g.
static ArrayList<String> getRecursiveDirectoryListing(String dirPath)
          Get a list of all of the fully qualified filename for all files in all subdirectories of the specified directory.
static void main(String[] args)
          A little test method that finds and displays all of the sub-classes of dlife.robot.Robot.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileSystemUtilities

public FileSystemUtilities()
Method Detail

getDLifeClassPathEntry

public static String getDLifeClassPathEntry()
Get the entry from the current classpath that corresponds to the dlife package. The dlife package may be included in the classpath as a directory or as a jar file.

Returns:
the classpath entry for the dlife package.

getDlifeHome

public static String getDlifeHome(String classPath)
Given the dLife class path entry this method finds dlife's home directory.

Returns:
the dLife home directory.

getAllDLifeSubClasses

public static ArrayList<String> getAllDLifeSubClasses(String superClass)
Get a list the fully qualified names of all of the concrete sub classes of the specified super class within the dlife package. This method omits all abstract sub-classes.

Parameters:
superClass - the fully qualified name of the super class (e.g. dlife.robot.Robot).
Returns:
An ArrayList of Strings holding all of the subclass names.

getRecursiveDirectoryListing

public static ArrayList<String> getRecursiveDirectoryListing(String dirPath)
Get a list of all of the fully qualified filename for all files in all subdirectories of the specified directory. Note that directory names are not included in the list that is returned.

Parameters:
dirPath - the path to the directory to recurse.
Returns:
An ArrayList of Strings holding all of the fully qualified names.

getDirectoryListing

public static String[] getDirectoryListing(String dirPath)
Get a list of all of the files in the specified directory. The list contains just the filenames of the files in the directory, not the entire path for each file.

Parameters:
dirPath - the directory for which to get the listing.
Returns:
an array of Strings holding the filenames of all of the files in the specified directory.

main

public static void main(String[] args)
A little test method that finds and displays all of the sub-classes of dlife.robot.Robot.

Parameters:
args - none.

getPathAndName

public static String[] getPathAndName(String fullPath)
                               throws ClassNotFoundException
Given an absolute path to a .class file, this method returns an Array of two Strings containing the classpath to the package that holds the class and the fully qualified class name. E.g. /lib/java/java1.5/java/lang/String.class would produce the classpath /lib/java/java1.5/ and the name java.lang.String. The classpath is returned in the first element of the array and the name is returned in the second element of the array. If the class file is not contained in any package then everything except the class name will be included in the classpath. E.g. /home/myfiles/Test.class would produce the classpath /home/myfiles/ and the name Test.

Parameters:
fullPath - the absolute pathname to the .class file.
Returns:
the classpath and the fully qualified class name.
Throws:
ClassNotFoundException - if the fullPath can not successfully be split into a path and a fully qualified class name. This should only ever happen if the specified fullPath does not point to a valid class file.
IllegalArgumentException - if the fullPath specified is not a full path.

getPathFromBinaryName

public static String getPathFromBinaryName(String binaryName)
Given a binary name for a class (e.g. dlife.robot.Hemisson), produce a string that contains the path to that file from the root directory of the package (e.g. dlife/robot/Hemisson.class).

Returns:
the path within a package to the the specified class.

getFilename

public static String getFilename(String fullPath)
Get the filename from a full path.

Parameters:
fullPath - the full path to the file.
Returns:
the filename portion of the path.
Throws:
IllegalArgumentException - if fullPath ends in a /

getPath

public static String getPath(String fullPath)
Get the path from a full path to a file.

Parameters:
fullPath - the full path to the file
Returns:
the path portion of the path including the final /.
Throws:
IllegalArgumentException - if fullPath ends in a /

getPathForJavaSourceFile

public static String getPathForJavaSourceFile(String javaSourceFile)
Determine the path to the specified java source file accounting for the possibility that the java source file contains a package statement. For example if the file is located at /home/user/abc/file1.java and file1.java contains the statement package abc; then the returned path would be /home/user/ However if file1.java does not contain a package statement then the returned path would be /home/user/abc/.

Parameters:
javaSourceFile - the path to a java source code file.
Returns:
the path to the file as described above (including the final /).
Throws:
IllegalArgumentException - if the file does not exist.

deleteDirectory

public static void deleteDirectory(String path)
Delete the specified directory and all of its contents.

Parameters:
path - the path to the directory.
Throws:
IllegalArgumentException - if path is not a directory.

copyFile

public static void copyFile(String f1,
                            String f2)
                     throws IOException
Copy the contents of one file to another file.

Parameters:
f1 - the full path to the file to be copied.
f2 - the full path of the new file to be created. If f2 exists its contents will be replaced by those of f1.
Throws:
IOException - if there is an error copying the file.
FileNotFoundException - if file f1 does not exist.

dLife Home Page