edu.neu.ccs.util
Class FileUtilities

java.lang.Object
  extended byedu.neu.ccs.util.FileUtilities

public class FileUtilities
extends Object

Provides utility methods that encapsulate file input and output operations.

Since:
1.0
Version:
2.4.0

Constructor Summary
private FileUtilities()
          Prevent instantiation.
 
Method Summary
static void copyFile(File source, File destination, boolean overwrite)
          Copies the source file to the destination file.
static void copyFile(String source, String destination, boolean overwrite)
          Copies the file at the source path to the destination path.
static void createDirectory(File dir)
          Creates the given directory.
static void createDirectory(String dir)
          Creates a directory with the given path.
static void deleteFile(File f)
          Deletes the given file.
static void deleteFile(String path)
          Deletes the file at the given path.
static String[] filterFileList(String[] fileList, String extensionList)
          Assuming the given file list array contains a list of file names, return the sub-list of those names whose extensions match those in the given extensions list; the extensions list is a comma separated list that is parsed to extract the extensions; case is ignored in the comparison.
static String[] filterFileList(String[] fileList, String[] extensionList)
          Assuming the given file list array contains a list of file names, return the sub-list of those names whose extensions match those in the given extensions list; the extensions list consists of an array of file extensions; case is ignored in the comparison.
static StringBuffer getFileContents(File f)
          Returns a StringBuffer containing the contents of the given file.
static boolean matchesExtension(String fileName, String fileExtension)
          Returns true if the given file name matches the given file extension with case ignored.
static String[] readDirectory(File f)
          Returns an array of Strings containing the names of the files in the given directory.
static String[] readDirectory(String path)
          Returns an array of Strings containing the names of the files in the directory at the given path.
static String readFile(File f)
          Returns a String containing the contents of the given file.
static String readFile(String path)
          Returns a String containing the contents of the file at the given path.
static void renameFile(File source, File destination)
          Sets the name of the given file to the provided name.
static void renameFile(String source, String destination)
          Renames the file at the given path to the given new path.
static void writeFile(File f, String data, boolean overwrite)
          Writes the given data to the given file.
static void writeFile(String destination, String data, boolean overwrite)
          Writes the given data to a file at the given path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtilities

private FileUtilities()
Prevent instantiation.

Method Detail

copyFile

public static void copyFile(String source,
                            String destination,
                            boolean overwrite)
                     throws FileExistsException,
                            IOException
Copies the file at the source path to the destination path.

Parameters:
source - the path of the source file to copy
destination - the path to copy the file to
overwrite - whether or not to automatically overwrite existing file contents
Throws:
FileExistsException - if the file exists and is not to be automatically overwritten
IOException - if there is an error reading the source file or writing the destination file
See Also:
copyFile(File, File, boolean)

copyFile

public static void copyFile(File source,
                            File destination,
                            boolean overwrite)
                     throws FileExistsException,
                            IOException
Copies the source file to the destination file.

Parameters:
source - a source file to be copied
destination - the desination file to be written
overwrite - whether or not to automatically overwrite existing file contents
Throws:
FileExistsException - if the file exists and is not to be automatically overwritten
IOException - if there is an error reading the source file or writing the destination file
See Also:
copyFile(String, String, boolean)

createDirectory

public static void createDirectory(File dir)
                            throws FileExistsException,
                                   IOException
Creates the given directory.

Parameters:
dir - the directory to be created
Throws:
FileExistsException - if the file or directory already exists
IOException - if there is an error creating the directory
See Also:
createDirectory(String)

createDirectory

public static void createDirectory(String dir)
                            throws FileExistsException,
                                   IOException
Creates a directory with the given path.

Parameters:
dir - the full path of the new directory
Throws:
FileExistsException - if the file or directory already exists
IOException - if there is an error creating the directory
See Also:
createDirectory(File)

deleteFile

public static void deleteFile(File f)
                       throws FileNotFoundException,
                              IOException
Deletes the given file.

Parameters:
f - the file to be deleted
Throws:
FileNotFoundException - if the file does not exist
IOException - if there was an error deleting the file
See Also:
deleteFile(String)

deleteFile

public static void deleteFile(String path)
                       throws FileNotFoundException,
                              IOException
Deletes the file at the given path.

Parameters:
path - the path of the file to be deleted
Throws:
FileNotFoundException - if the file does not exist
IOException - if there was an error deleting the file
See Also:
deleteFile(File)

readDirectory

public static String[] readDirectory(File f)
                              throws FileNotFoundException,
                                     IOException
Returns an array of Strings containing the names of the files in the given directory.

Parameters:
f - the directory to be read
Throws:
FileNotFoundException - if the directory does not exist
IOException - if there was an error reading the directory
See Also:
readDirectory(String)

readDirectory

public static String[] readDirectory(String path)
                              throws FileNotFoundException,
                                     IOException
Returns an array of Strings containing the names of the files in the directory at the given path.

Parameters:
path - the full path of the directory
Throws:
FileNotFoundException - if the directory does not exist
IOException - if there was an error reading the directory
See Also:
readDirectory(File)

filterFileList

public static String[] filterFileList(String[] fileList,
                                      String[] extensionList)

Assuming the given file list array contains a list of file names, return the sub-list of those names whose extensions match those in the given extensions list; the extensions list consists of an array of file extensions; case is ignored in the comparison.

Returns null if the file list is null.

Returns the file list, if the extensions list is null.

Parameters:
fileList - the file list to filter
extensionList - the extensions to define the filter

filterFileList

public static String[] filterFileList(String[] fileList,
                                      String extensionList)

Assuming the given file list array contains a list of file names, return the sub-list of those names whose extensions match those in the given extensions list; the extensions list is a comma separated list that is parsed to extract the extensions; case is ignored in the comparison.

Returns null if the file list is null.

Returns the file list, if the extensions list is null.

Parameters:
fileList - the file list to filter
extensionList - the extensions in a comma separated list

matchesExtension

public static boolean matchesExtension(String fileName,
                                       String fileExtension)

Returns true if the given file name matches the given file extension with case ignored.

Returns false if either argument is null or if the file extension string is of length 0.

Parameters:
fileName - the file name
fileExtension - the extension to match

getFileContents

public static StringBuffer getFileContents(File f)
                                    throws FileNotFoundException,
                                           IOException
Returns a StringBuffer containing the contents of the given file.

Parameters:
f - the file to be read
Throws:
FileNotFoundException - if the file does not exist
IOException - if there was an error reading the file
See Also:
readFile(File), readFile(String)

readFile

public static String readFile(File f)
                       throws FileNotFoundException,
                              IOException
Returns a String containing the contents of the given file.

Parameters:
f - the file to be read
Throws:
FileNotFoundException - if the file does not exist
IOException - if there was an error reading the file
See Also:
readFile(String)

readFile

public static String readFile(String path)
                       throws FileNotFoundException,
                              IOException
Returns a String containing the contents of the file at the given path.

Parameters:
path - the path of the file to be read
Throws:
FileNotFoundException - if the file does not exist
IOException - if there was an error reading the file
See Also:
readFile(File)

renameFile

public static void renameFile(File source,
                              File destination)
                       throws FileNotFoundException,
                              FileExistsException,
                              IOException
Sets the name of the given file to the provided name.

Parameters:
source - the file to be renamed
destination - the new name for the file
Throws:
FileNotFoundException - if the source file is not found
FileExistsException - if the destination file already exists and will be overwritten
IOException - if there is an error renaming the file
See Also:
renameFile(String, String)

renameFile

public static void renameFile(String source,
                              String destination)
                       throws FileNotFoundException,
                              FileExistsException,
                              IOException
Renames the file at the given path to the given new path.

Parameters:
source - the full path of the file to be renamed
destination - the full path of the new name for the file
Throws:
FileNotFoundException - if a file at the source path does not exist
FileExistsException - if a file exists at the destination path and will be overwritten
IOException - if there is an error renaming the file
See Also:
renameFile(File, File)

writeFile

public static void writeFile(File f,
                             String data,
                             boolean overwrite)
                      throws FileExistsException,
                             IOException
Writes the given data to the given file.

Parameters:
f - the file to be written
data - the data to write to the file
overwrite - whether or not to automatically overwrite existing file contents
Throws:
FileExistsException - if the file exists and is not to be automatically overwritten
IOException - if there is an error writing the file
See Also:
writeFile(String, String, boolean)

writeFile

public static void writeFile(String destination,
                             String data,
                             boolean overwrite)
                      throws FileExistsException,
                             IOException
Writes the given data to a file at the given path.

Parameters:
destination - the full path of the file to be written
data - the data to write to the file
overwrite - whether or not to automatically overwrite existing file contents
Throws:
FileExistsException - if the file exists and is not to be automatically overwritten
IOException - if there is an error writing the file
See Also:
writeFile(File, String, boolean)