org.igfay.util
Class FileUtility

java.lang.Object
  extended byorg.igfay.util.FileUtility

public class FileUtility
extends java.lang.Object

Helper methods for working with File objects

Author:
bconrad

Field Summary
protected static Logger log
           
 
Constructor Summary
FileUtility()
           
 
Method Summary
static void closeStreams(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Close the provided input and output streams.
static java.lang.String contentsOfFile(java.io.File file)
          Return the contents of the provided File as a String
static java.lang.String contentsOfFile(java.lang.String fileName)
          Return the contents of the provided file name as a String
static void copy(java.io.File inFile, java.io.File outFile)
           
static void copy(java.io.InputStream in, java.io.OutputStream out)
           
static boolean ensureFilePathExists(java.io.File file)
          Ensure that the provided file exists.
static boolean ensureFilePathExists(java.lang.String fileString)
          Ensure that the provided file name exists.
static boolean ensurePathExists(java.io.File file)
          Ensure that the path to the provided file exists.
static boolean ensurePathExists(java.lang.String fileString)
          Ensure that the path to the provided file name exists.
static java.io.File findExistingFile(java.lang.String fileName)
          Return a File for an existing file.
static java.io.BufferedReader getBufferedReaderFromFile(java.io.File file)
          Return the provided File as a BufferedReader
static java.io.BufferedReader getBufferedReaderFromFile(java.lang.String fileString)
          Return the provided file name as a BufferedReader
static java.lang.String getExceptionMessage(java.lang.Exception e, java.io.File file)
           
static java.lang.String getLogFileName()
          Return the name of the log file.
static void iteratorToFile(java.util.Iterator it, java.lang.String fileName)
          Write the provided iterator to a file of the provided file name.
static java.io.PrintStream newPrintStreamOnFileNamed(java.io.File directory, java.lang.String name)
           
static java.io.PrintWriter newPrintWriterOnFileNamed(java.io.File directory, java.lang.String name)
          Create and return a PrintWriter for the provided directory and file name.
static java.lang.Object readObjectFromBufferedFileObject(java.io.File file)
          Convert the provided File into a BufferedInputStream.
static java.lang.Object readObjectFromFile(java.io.File file)
          Read the provded file and return the contents as an Object.
static java.lang.Object readObjectFromFile(java.io.File directory, java.lang.String name)
          Read the contents of the provided file name residing in the provided directory.
static java.lang.Object readObjectFromFileWithPath(java.lang.String fileWithPath)
          Read the contents of the provided file name residing in the provided directory.
static void redirectStandardOutput()
          Redirect standard output to the log file.
static void redirectStandardOutput(java.io.File file)
          Redirect standard output to the provided File.
static void redirectStandardOutput(java.lang.String fileName)
          Redirect standard output to the provided file name.
static java.lang.String streamToString(java.io.InputStream inputStream)
          Return the provided InputStream as a String
static java.lang.String stringFromFile(java.io.File file)
          Return the contents of the provided file as a String.
static java.lang.String stringFromFile(java.lang.String fileName)
          Return the contents of the provided file name as a String.
static void stringToFile(java.lang.String string, java.io.File file)
          Convert the provided String into the provided File.
static void stringToFile(java.lang.String string, java.lang.String fileString)
          Convert the provided String into a File of the provided file name.
static void stringToFileOutputStream(java.lang.String string, java.io.FileOutputStream fos)
          Convert the provided String into the provided FileOutputStream.
static void writeObjectToFile(java.lang.Object object, java.io.File directory, java.lang.String name)
          Write the provided object to a file of the provided directory and file name.
static void writeObjectToFileObject(java.lang.Object object, java.io.File file)
          Write the provided object to the provided File.
static void writeObjectToFileWithPath(java.lang.Object object, java.lang.String fileAndPath)
          Write the provided object to the provided file name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static Logger log
Constructor Detail

FileUtility

public FileUtility()
Method Detail

contentsOfFile

public static java.lang.String contentsOfFile(java.io.File file)
Return the contents of the provided File as a String

Parameters:
file -
Returns:

contentsOfFile

public static java.lang.String contentsOfFile(java.lang.String fileName)
Return the contents of the provided file name as a String

Parameters:
fileName -
Returns:

copy

public static void copy(java.io.File inFile,
                        java.io.File outFile)
                 throws java.io.IOException
Throws:
java.io.IOException

copy

public static void copy(java.io.InputStream in,
                        java.io.OutputStream out)
                 throws java.io.IOException
Throws:
java.io.IOException

closeStreams

public static void closeStreams(java.io.InputStream inputStream,
                                java.io.OutputStream outputStream)
Close the provided input and output streams. Ignore any exception.

Parameters:
inputStream -
outputStream -

ensureFilePathExists

public static boolean ensureFilePathExists(java.io.File file)
Ensure that the provided file exists. If it does not, create it along with any necessary directories in the path.

Parameters:
file -
Returns:

ensurePathExists

public static boolean ensurePathExists(java.io.File file)
Ensure that the path to the provided file exists. If it does not, create it.

Parameters:
file -
Returns:

ensureFilePathExists

public static boolean ensureFilePathExists(java.lang.String fileString)
Ensure that the provided file name exists. If it does not, create it along with any necessary directories in the path.

Parameters:
fileString -
Returns:

ensurePathExists

public static boolean ensurePathExists(java.lang.String fileString)
Ensure that the path to the provided file name exists. If it does not, create it.

Parameters:
fileString -
Returns:

newPrintStreamOnFileNamed

public static java.io.PrintStream newPrintStreamOnFileNamed(java.io.File directory,
                                                            java.lang.String name)
                                                     throws java.io.IOException
Throws:
java.io.IOException

newPrintWriterOnFileNamed

public static java.io.PrintWriter newPrintWriterOnFileNamed(java.io.File directory,
                                                            java.lang.String name)
                                                     throws java.io.IOException
Create and return a PrintWriter for the provided directory and file name.

Parameters:
directory -
name -
Returns:
Throws:
java.io.IOException

readObjectFromBufferedFileObject

public static java.lang.Object readObjectFromBufferedFileObject(java.io.File file)
                                                         throws java.io.IOException,
                                                                java.lang.ClassNotFoundException
Convert the provided File into a BufferedInputStream. Then read the stream and return the Object that was read.

Parameters:
file -
Returns:
Throws:
java.io.IOException
java.lang.ClassNotFoundException

readObjectFromFile

public static java.lang.Object readObjectFromFile(java.io.File file)
                                           throws java.io.IOException,
                                                  java.lang.ClassNotFoundException
Read the provded file and return the contents as an Object.

Parameters:
file - java.lang.String
Returns:
java.lang.Object
Throws:
java.io.IOException - Description of Exception
java.lang.ClassNotFoundException - Description of Exception

readObjectFromFile

public static java.lang.Object readObjectFromFile(java.io.File directory,
                                                  java.lang.String name)
                                           throws java.io.IOException,
                                                  java.lang.ClassNotFoundException
Read the contents of the provided file name residing in the provided directory. Return the contents as an Object.

Parameters:
directory -
name -
Returns:
java.lang.Object
Throws:
java.io.IOException
java.lang.ClassNotFoundException

readObjectFromFileWithPath

public static java.lang.Object readObjectFromFileWithPath(java.lang.String fileWithPath)
                                                   throws java.io.IOException,
                                                          java.lang.ClassNotFoundException
Read the contents of the provided file name residing in the provided directory. Return the contents as an Object.

Parameters:
fileWithPath -
Returns:
Throws:
java.io.IOException
java.lang.ClassNotFoundException

redirectStandardOutput

public static void redirectStandardOutput()
Redirect standard output to the log file.


redirectStandardOutput

public static void redirectStandardOutput(java.io.File file)
Redirect standard output to the provided File.

Parameters:
file -

redirectStandardOutput

public static void redirectStandardOutput(java.lang.String fileName)
Redirect standard output to the provided file name.


stringFromFile

public static java.lang.String stringFromFile(java.io.File file)
Return the contents of the provided file as a String.

Parameters:
file -
Returns:

stringFromFile

public static java.lang.String stringFromFile(java.lang.String fileName)
Return the contents of the provided file name as a String.

Returns:

stringToFile

public static void stringToFile(java.lang.String string,
                                java.io.File file)
Convert the provided String into the provided File.

Parameters:
string -
file -

stringToFile

public static void stringToFile(java.lang.String string,
                                java.lang.String fileString)
Convert the provided String into a File of the provided file name.

Parameters:
string -

stringToFileOutputStream

public static void stringToFileOutputStream(java.lang.String string,
                                            java.io.FileOutputStream fos)
                                     throws java.io.IOException
Convert the provided String into the provided FileOutputStream.

Parameters:
string -
fos -
Throws:
java.io.IOException

writeObjectToFile

public static void writeObjectToFile(java.lang.Object object,
                                     java.io.File directory,
                                     java.lang.String name)
                              throws java.io.IOException
Write the provided object to a file of the provided directory and file name.

Parameters:
object -
directory -
name -
Throws:
java.io.IOException

writeObjectToFileObject

public static void writeObjectToFileObject(java.lang.Object object,
                                           java.io.File file)
                                    throws java.io.IOException
Write the provided object to the provided File.

Parameters:
object -
file -
Throws:
java.io.IOException

writeObjectToFileWithPath

public static void writeObjectToFileWithPath(java.lang.Object object,
                                             java.lang.String fileAndPath)
                                      throws java.io.IOException
Write the provided object to the provided file name.

Parameters:
object -
fileAndPath -
Throws:
java.io.IOException

iteratorToFile

public static void iteratorToFile(java.util.Iterator it,
                                  java.lang.String fileName)
                           throws java.io.IOException
Write the provided iterator to a file of the provided file name.

Parameters:
it -
fileName -
Throws:
java.io.IOException

getBufferedReaderFromFile

public static java.io.BufferedReader getBufferedReaderFromFile(java.io.File file)
                                                        throws java.io.FileNotFoundException
Return the provided File as a BufferedReader

Parameters:
file -
Returns:
Throws:
java.io.FileNotFoundException

getBufferedReaderFromFile

public static java.io.BufferedReader getBufferedReaderFromFile(java.lang.String fileString)
                                                        throws java.io.FileNotFoundException
Return the provided file name as a BufferedReader

Parameters:
fileString -
Returns:
Throws:
java.io.FileNotFoundException

getExceptionMessage

public static java.lang.String getExceptionMessage(java.lang.Exception e,
                                                   java.io.File file)

getLogFileName

public static java.lang.String getLogFileName()
Return the name of the log file.

Returns:

findExistingFile

public static java.io.File findExistingFile(java.lang.String fileName)
Return a File for an existing file. If we can't find it by the fileName, find it in the classpath. If that fails, return null.

Parameters:
fileName -
Returns:

streamToString

public static java.lang.String streamToString(java.io.InputStream inputStream)
Return the provided InputStream as a String

Parameters:
inputStream -
Returns: