com.foxsmart.ic.io
Class IoUtils

java.lang.Object
  extended by com.foxsmart.ic.io.IoUtils

public class IoUtils
extends java.lang.Object

This class provides various I/O utilities via static methods.


Field Summary
protected static org.apache.commons.logging.Log log
           
 
Constructor Summary
IoUtils()
           
 
Method Summary
static java.lang.Object clone(java.lang.Object object)
          Clones the passed in object by serializing and deserializing all the serialized contents of the object.
static void createDirectory(java.lang.String directoryName)
          Creates a directory if one doesn't already exist for the specified directory name.
static void createDirectory(java.lang.String filename, boolean isFile)
          Creates a directory if one doesn't already exist based on a filename.
static void deleteFile(java.io.File file, boolean recursiveDelete)
          Removes a file, a directory, or recursive directories.
static java.lang.String getFileContents(java.io.File file)
          Reads and returns the contents of the specified file.
static java.lang.String getFileContents(java.lang.String filename)
          Reads and returns the contents of the specified file.
static java.util.List<java.io.File> getFiles(java.lang.String filename)
          Gets a list of files based on the passed in filename.
static java.lang.String getFullPath(java.lang.String directoryName, java.lang.String filename)
          Returns the full path by combining the directory name and the filename.
static void writeFile(java.io.File file, java.lang.String fileContents)
          Writes the file contents to the specified file.
static boolean writeFile(java.lang.String directory, java.lang.String filename, java.lang.String fileContents, boolean createDirectory, boolean overwrite)
          Writes the file contents to the specified directory and filename.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log
Constructor Detail

IoUtils

public IoUtils()
Method Detail

deleteFile

public static void deleteFile(java.io.File file,
                              boolean recursiveDelete)
                       throws java.lang.Exception
Removes a file, a directory, or recursive directories. If the passed in File is a single file, it will be deleted. If the passed in File is a directory and the recursive flag is not set, it will remove all files in the passed in directory only. The directory itself will only be deleted if there were no other directories contained within the passed in directory. If the passed in File is a directory and the recursive flag is set, it will remove the directory and all of its contents including all subdirectories and their contents. Be careful when deleting directories recursively since this is a very destructive operation.

Parameters:
file - The file or directory to delete
recursiveDelete - Flag to indicate whether the directory that was passed in should be recursively deleted. This parameter is ignored if the passed in File is a normal file.
Throws:
java.lang.Exception - if there was a problem deleting any file that should have been deleted. Note that if an exception is thrown, several files may have already been deleted and will not be restored.

createDirectory

public static void createDirectory(java.lang.String directoryName)
                            throws java.io.IOException
Creates a directory if one doesn't already exist for the specified directory name. This method is the same thing as calling createDirectory(directoryName, false).

Parameters:
directoryName - The name of the directory to create
Throws:
java.io.IOException - if the directory could not be created.

createDirectory

public static void createDirectory(java.lang.String filename,
                                   boolean isFile)
                            throws java.io.IOException
Creates a directory if one doesn't already exist based on a filename. The filename can be a file or a directory based on the isFile flag. For example, if the filename is "test1/test2" and isFile is true, the directory "test1" will be created if it doesn't already exist. If the filename is "test1/test2" and isFile is false, the directory "test1/test2" will be created if it doesn't already exist.

Parameters:
filename - The filename of the directory of file.
isFile - If true, the filename specifies a file. If false, the filename specifies a directory.
Throws:
java.io.IOException - if the directory could not be created.

getFiles

public static java.util.List<java.io.File> getFiles(java.lang.String filename)
                                             throws java.lang.Exception
Gets a list of files based on the passed in filename. If the entry is a directory, all files in the directory will be returned. If the entry is a filename, only the passed in file will be returned.

Parameters:
filename - the filename.
Returns:
The list of files as individual File objects in a List.
Throws:
java.lang.Exception - if there is a problem processing the filename.

getFileContents

public static java.lang.String getFileContents(java.lang.String filename)
                                        throws java.io.IOException
Reads and returns the contents of the specified file.

Parameters:
filename - The file whose contents will be read.
Returns:
The file contents.
Throws:
java.io.IOException - if the file doesn't exist or it's contents could not be read.

getFileContents

public static java.lang.String getFileContents(java.io.File file)
                                        throws java.io.IOException
Reads and returns the contents of the specified file.

Parameters:
file - The file whose contents will be read.
Returns:
The file contents.
Throws:
java.io.IOException - if the file doesn't exist or it's contents could not be read.

clone

public static java.lang.Object clone(java.lang.Object object)
                              throws java.lang.Exception
Clones the passed in object by serializing and deserializing all the serialized contents of the object.

Parameters:
object - The object to clone
Returns:
The cloned object.
Throws:
java.lang.Exception - if the object could not be cloned.

getFullPath

public static java.lang.String getFullPath(java.lang.String directoryName,
                                           java.lang.String filename)
Returns the full path by combining the directory name and the filename.

Parameters:
directoryName - the directory name.
filename - the filename.
Returns:
the full path.

writeFile

public static boolean writeFile(java.lang.String directory,
                                java.lang.String filename,
                                java.lang.String fileContents,
                                boolean createDirectory,
                                boolean overwrite)
                         throws java.io.IOException
Writes the file contents to the specified directory and filename.

Parameters:
directory - the directory of the file to create.
filename - the filename of the file to create.
fileContents - the contents of the file to write.
createDirectory - If true, the directory will be created. Otherwise, no directory will be created.
overwrite - If true, the an existing file will be overwritten. Othewise, the file will not be written.
Returns:
true if the file was written or false if not (e.g. it already exists).
Throws:
java.io.IOException - if any problems were encountered writing to the file.

writeFile

public static void writeFile(java.io.File file,
                             java.lang.String fileContents)
                      throws java.io.IOException
Writes the file contents to the specified file.

Parameters:
file - the file that will contain the file contents.
fileContents - the file contents to write to the file.
Throws:
java.io.IOException - if any problems were encountered writing to the file.


Copyright © 2005-2008 Fox Smart, Inc. All Rights Reserved.