Expresso 5-6

com.jcorporate.expresso.core.misc
Class FileUtil

java.lang.Object
  extended bycom.jcorporate.expresso.core.misc.FileUtil

public class FileUtil
extends Object

Utilities for manipulations of disk files

Author:
Michael Nash

Constructor Summary
FileUtil()
          Constructor
 
Method Summary
static void cleanDirs(String fileName)
          CleanDirs is a utility method for cleaning up temporary directories, used by various methods to hold & process files on the file system.
static void copyFile(String sourceFile, String destFile)
          Utility method to copy a file from one place to another & delete the original.
static void copyStream(InputStream is, OutputStream os)
          CopyStream does just what it says: It copies the entire contents of the input stream to the output stream by using reading and writing in 4k chunks.
static String getBase(String fileName)
          Get the base filename of a file (e.g. no directory or extension)
static Vector getDir(String dirName)
          Return a vector of the file/dir names in any give directory
static String getExtension(String fileName)
          Get the file extension (after the ".")
static Vector getFileListingRecursive(String dirName, String prefix, String separator, Vector includeExtension)
          Return a vector of the recursive listing of the file names starting from in any given directory
static String getPath(String fileName)
          Get the path of a file name
static File getTempDirectory(String dataContext)
          Retrieve the temp directory in the form of a java.io.File object.
static void main(String[] args)
          Command line interface to run specific items [Also used for unit testing]
static String makeAbsolutePath(String prefix, String originalPath)
          Take a prefix and a relative path and put the two together to make an absolute path
static void moveFile(String sourceFile, String destFile)
          Copy a file, then remove the original file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Constructor

Method Detail

getTempDirectory

public static File getTempDirectory(String dataContext)
                             throws ConfigurationException
Retrieve the temp directory in the form of a java.io.File object. This then can be used as a 'parent' for creating files within that directory via the createTempFile factory method in java.io.File.

Parameters:
dataContext - the location to look for the setup table
Returns:
File instance
Throws:
ConfigurationException - if the specified data context cannot be found

getBase

public static String getBase(String fileName)
Get the base filename of a file (e.g. no directory or extension)

Parameters:
fileName - Original pathname to get the base name from. IMPORTANT: This method assumes that "/" is the directory seperator, not "\"!!!
Returns:
String The base file name

getDir

public static Vector getDir(String dirName)
                     throws IOException
Return a vector of the file/dir names in any give directory

Parameters:
dirName - the name of the directory to retrieve a list of all files
Returns:
Vector of strings of file names in the directory
Throws:
IOException - If the given name is not a directory or if an I/O error occurs when trying to read the file list

getFileListingRecursive

public static Vector getFileListingRecursive(String dirName,
                                             String prefix,
                                             String separator,
                                             Vector includeExtension)
                                      throws IOException
Return a vector of the recursive listing of the file names starting from in any given directory

Parameters:
dirName - the name of the directory to retrieve a list of all files in that directory and it's sub-directories
prefix - a string to prepend to the path
includeExtension - List of strings of files to be included. All are included if the list is null.
Returns:
List of strings of file names in the directory and sub-directories
Throws:
IOException - If the given dirName is not a directory or if an I/O error occurs when trying to read the file list

getExtension

public static String getExtension(String fileName)
Get the file extension (after the ".")

Parameters:
fileName - Original full file name
Returns:
String Extension name

getPath

public static String getPath(String fileName)
Get the path of a file name

Parameters:
fileName - Original pathname
Returns:
String Path portion of the pathname

cleanDirs

public static void cleanDirs(String fileName)
                      throws IOException
CleanDirs is a utility method for cleaning up temporary directories, used by various methods to hold & process files on the file system. CleanDir takes a directory name as an argument, and checks for any empty directories within that directory. If it finds any, it remove the empty directory & checks again until no empty directories are found.

Parameters:
fileName - the name of the directory to clean
Throws:
IOException - upon error

copyFile

public static void copyFile(String sourceFile,
                            String destFile)
                     throws IOException
Utility method to copy a file from one place to another & delete the original. We do this in order to preserve directory security, which is not preserved under NT if we simply move the file (with a renameTo) If we fail, we throw IOException, also reporting what user this process is running with to assist System Admins in setting appropriate permissions

Parameters:
sourceFile - Source file pathname
destFile - Destination file pathname
Throws:
IOException - If the copy fails due to an I/O error

main

public static void main(String[] args)
Command line interface to run specific items [Also used for unit testing]

Parameters:
args - the command line arguments

makeAbsolutePath

public static String makeAbsolutePath(String prefix,
                                      String originalPath)
Take a prefix and a relative path and put the two together to make an absolute path

Parameters:
prefix - the prefix of the path
originalPath - the original pathname
Returns:
java.lang.String the absolute path

moveFile

public static void moveFile(String sourceFile,
                            String destFile)
                     throws IOException
Copy a file, then remove the original file

Parameters:
sourceFile - Original file name
destFile - Destination file name
Throws:
IOException - If an I/O error occurs during the copy

copyStream

public static void copyStream(InputStream is,
                              OutputStream os)
                       throws IOException
CopyStream does just what it says: It copies the entire contents of the input stream to the output stream by using reading and writing in 4k chunks. This particular method has been shown to be the best option for higher performance stream copying.

copyStream flushes the output buffer when it has completed the copy, but does not close the input stream or output stream. That is the calling function's responsibility.

Parameters:
is - the input stream for the source of the copy
os - the output stream for the sink of the copy
Throws:
IOException - upon error

Expresso 5-6

Please see www.jcorporate.com for information about new Expresso releases.