edu.rice.cs.cunit.util
Class FileOps

java.lang.Object
  extended by edu.rice.cs.cunit.util.FileOps

public class FileOps
extends java.lang.Object

File Operations.

Author:
Mathias Ricken

Nested Class Summary
static class FileOps.FileOpsTest
           
 
Constructor Summary
FileOps()
           
 
Method Summary
static java.lang.String convertToAbsolutePathEntries(java.lang.String path)
          Convert all path entries in a path string to absolute paths.
static void copyFile(java.io.File src, java.io.File dst)
          Copy the the file or directory src to dst.
static java.io.File createTempDirectory(java.lang.String name)
          Create a new temporary directory.
static java.io.File createTempDirectory(java.lang.String name, java.io.File parent)
          Create a new temporary directory.
static boolean deleteDirectory(java.io.File dir)
          Delete the given directory including any files and directories it contains.
static void deleteDirectoryOnExit(java.io.File dir)
          Instructs Java to recursively delete the given directory and its contents when the JVM exits.
static java.util.Set<java.io.File> enumFiles(java.io.File src)
          Enumerate the files in src.
static void exit(int status)
          Unequivocally exits a program, if necessary by using Runtime.halt and not executing ShutdownHooks.
static boolean isContainedIn(java.io.File f, java.io.File dir)
          Returns true if the file f is contained in the directory dir or its subdirectories.
static boolean isContainedInCanonical(java.io.File f, java.io.File dir)
          Returns true if the file f is contained in the directory dir or its subdirectories, or if the file *is* the directory.
static java.io.File makeRelativeTo(java.io.File f, java.io.File b)
          Makes a file equivalent to the given file f that is relative to base file b.
static long packJar(java.io.File inDir, java.io.File outJar)
          Pack all files in the directory into the specified Jar.
static java.lang.String[] splitFile(java.io.File fileToSplit)
          Splits a file into an array of strings representing each parent folder of the given file.
static java.lang.String[] splitPaths(java.lang.String pathString, char pathSeparator)
          Splits a string with a list of paths, separated by pathSeparator, into an array of paths.
static java.lang.String[] splitPaths(java.lang.String pathString, char pathSeparator, boolean winDriveLetters)
          Splits a string with a list of paths, separated by pathSeparator, into an array of paths.
static long unpackJar(java.io.File inJar, java.io.File outDir)
          Unpack all files in the jar file to the specified directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileOps

public FileOps()
Method Detail

makeRelativeTo

public static java.io.File makeRelativeTo(java.io.File f,
                                          java.io.File b)
                                   throws java.io.IOException
Makes a file equivalent to the given file f that is relative to base file b. In other words, new File(b,makeRelativeTo(base,abs)).getCanonicalPath() equals f.getCanonicalPath()

In Linux/Unix, if the file f is /home/username/folder/file.java and the file b is /home/username/folder/sublevel/file2.java, then the resulting File path from this method would be ../file.java while its canoncial path would be /home/username/folder/file.java.

In Windows, a file will be made absolute if it is on a different drive than the base.

Parameters:
f - The path that is to be made relative to the base file
b - The file to make the next file relative to
Returns:
A new file whose path is relative to the base file while the value of getCanonicalPath() for the returned file is the same as the result of getCanonicalPath() for the given file.
Throws:
java.io.IOException - if an I/O error occurs; may happen since getCanonicalFile uses the file system

isContainedIn

public static boolean isContainedIn(java.io.File f,
                                    java.io.File dir)
Returns true if the file f is contained in the directory dir or its subdirectories.

Parameters:
f - the file
dir - the directory
Returns:
true if file is contained in directory or one of its subdirectories

isContainedInCanonical

public static boolean isContainedInCanonical(java.io.File f,
                                             java.io.File dir)
Returns true if the file f is contained in the directory dir or its subdirectories, or if the file *is* the directory. Both the file and the directory must be canonical already.

Parameters:
f - the canonical file
dir - the canonical directory
Returns:
true if file is contained in directory or one of its subdirectories or the file *is* the directory

splitFile

public static java.lang.String[] splitFile(java.io.File fileToSplit)
Splits a file into an array of strings representing each parent folder of the given file. The file whose path is /home/username/txt.txt in linux would be split into the string array: {"","home","username","txt.txt"}. Delimeters are excluded.

Parameters:
fileToSplit - the file to split into its directories.
Returns:
array of path element names

splitPaths

public static java.lang.String[] splitPaths(java.lang.String pathString,
                                            char pathSeparator)
Splits a string with a list of paths, separated by pathSeparator, into an array of paths. This will correctly split a path even if it contains a drive letter, but only if File.pathSeparator ('/' or '\\') follows the drive letter. Examples (assuming the File.separatorChar is '\\'): splitPaths("C:\\foo:C:\\bar", ':', true) will be split into {"C:\\foo", "C:\\bar"}. splitPaths("C:foo:C:\\bar", ':', true) will be split into {"C", "foo", "C:\\bar"}.

Parameters:
pathString - the string with the list of paths, separated by pathSeparator
pathSeparator - the character separating the paths (should be either ':' or ';')
Returns:
array of paths

splitPaths

public static java.lang.String[] splitPaths(java.lang.String pathString,
                                            char pathSeparator,
                                            boolean winDriveLetters)
Splits a string with a list of paths, separated by pathSeparator, into an array of paths. If winDriveLetters is true, this will correctly split a path even if it contains a drive letter, but only if File.pathSeparator ('/' or '\\') follows the drive letter. Examples (assuming the File.separatorChar is '\\'): splitPaths("C:\\foo:C:\\bar", ':', true) will be split into {"C:\\foo", "C:\\bar"}. splitPaths("C:foo:C:\\bar", ':', true) will be split into {"C", "foo", "C:\\bar"}.

Parameters:
pathString - the string with the list of paths, separated by pathSeparator
pathSeparator - the character separating the paths (should be either ':' or ';')
winDriveLetters - treat "C:" followed by separatorChar as drive letter
Returns:
array of paths

exit

public static void exit(int status)
Unequivocally exits a program, if necessary by using Runtime.halt and not executing ShutdownHooks.

Parameters:
status - program's exit status

createTempDirectory

public static java.io.File createTempDirectory(java.lang.String name)
                                        throws java.io.IOException
Create a new temporary directory. The directory will be deleted on exit, if empty. (To delete it recursively on exit, use deleteDirectoryOnExit.)

Parameters:
name - Non-unique portion of the name of the directory to create.
Returns:
File representing the directory that was created.
Throws:
java.io.IOException

createTempDirectory

public static java.io.File createTempDirectory(java.lang.String name,
                                               java.io.File parent)
                                        throws java.io.IOException
Create a new temporary directory. The directory will be deleted on exit, if it only contains temp files and temp directories created after it. (To delete it on exit regardless of contents, call deleteDirectoryOnExit after constructing the file tree rooted at this directory. Note that createTempDirectory(..) is not much more helpful than mkdir() in this context (other than generating a new temp file name) because cleanup is a manual process.)

Parameters:
name - Non-unique portion of the name of the directory to create.
parent - Parent directory to contain the new directory
Returns:
File representing the directory that was created.
Throws:
java.io.IOException

deleteDirectory

public static boolean deleteDirectory(java.io.File dir)
Delete the given directory including any files and directories it contains.

Parameters:
dir - File object representing directory to delete. If, for some reason, this file object is not a directory, it will still be deleted.
Returns:
true if there were no problems in deleting. If it returns false, something failed and the directory contents likely at least partially still exist.

deleteDirectoryOnExit

public static void deleteDirectoryOnExit(java.io.File dir)
Instructs Java to recursively delete the given directory and its contents when the JVM exits.

Parameters:
dir - File object representing directory to delete. If, for some reason, this file object is not a directory, it will still be deleted.

copyFile

public static void copyFile(java.io.File src,
                            java.io.File dst)
                     throws java.io.IOException
Copy the the file or directory src to dst.

Parameters:
src - source file
dst - destination
Throws:
java.io.IOException

enumFiles

public static java.util.Set<java.io.File> enumFiles(java.io.File src)
                                             throws java.io.IOException
Enumerate the files in src.

Parameters:
src - source file
Returns:
list of files in src
Throws:
java.io.IOException

convertToAbsolutePathEntries

public static java.lang.String convertToAbsolutePathEntries(java.lang.String path)
Convert all path entries in a path string to absolute paths. The delimiter in the path string is the "path.separator" property. Empty entries are equivalent to "." and will thus are converted to the "user.dir" (working directory). Example: ".:drjava::/home/foo/junit.jar" with "user.dir" set to "/home/foo/bar" will be converted to "/home/foo/bar:/home/foo/bar/drjava:/home/foo/bar:/home/foo/junit.jar".

Parameters:
path - path string with entries to convert
Returns:
path string with all entries as absolute paths

unpackJar

public static long unpackJar(java.io.File inJar,
                             java.io.File outDir)
                      throws java.io.IOException
Unpack all files in the jar file to the specified directory.

Parameters:
inJar - the input Jar
outDir - the output directory
Returns:
number of files unpacked
Throws:
java.io.IOException

packJar

public static long packJar(java.io.File inDir,
                           java.io.File outJar)
                    throws java.io.IOException
Pack all files in the directory into the specified Jar.

Parameters:
inDir - the input directory
outJar - the output Jar
Returns:
number of files packed
Throws:
java.io.IOException