com.jmw.util
Class FileUtils

java.lang.Object
  extended by com.jmw.util.FileUtils

public class FileUtils
extends java.lang.Object

A group of static methods that are used for quick file manipulation, particularly reading and writing text files.


Constructor Summary
FileUtils()
           
 
Method Summary
static void bufferToFile(java.lang.StringBuffer sb, java.io.File file)
          Saves the contents of a StringBuffer to a file, using the default encoding.
static void bufferToFile(java.lang.StringBuffer sb, java.io.File file, java.lang.String encoding)
          Saves the contents of a StringBuffer to a file, using the specified encoding.
static void bufferToFile(java.lang.StringBuffer sb, java.lang.String fileName)
          Saves the contents of a StringBuffer to a file, using the default encoding.
static void bufferToFile(java.lang.StringBuffer sb, java.lang.String fileName, java.lang.String encoding)
          Saves the contents of a StringBuffer to a file, using the default encoding.
static java.lang.StringBuffer fileToStringBuffer(java.io.File file)
          Retrieves an entire file and loads it into a StringBuffer.
static java.lang.StringBuffer fileToStringBuffer(java.io.File file, java.lang.String encoding)
          Retrieves an entire file and loads it into a StringBuffer.
static java.lang.StringBuffer fileToStringBuffer(java.lang.String fileName)
          Retrieves an entire file and loads it into a StringBuffer.
static java.lang.StringBuffer fileToStringBuffer(java.lang.String fileName, java.lang.String encoding)
          Retrieves an entire file and loads it into a StringBuffer.
static java.io.File findSubDir(java.lang.String dirname)
           
static java.util.ArrayList<java.io.File> findSubDirectories(java.io.File root, int level, boolean leavesOnly)
          Recursivly finds all of the subdirectories of a given root.
static java.io.File getNewestLastModified(java.io.File dir)
          Finds the most recently modified file in the given directory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

fileToStringBuffer

public static java.lang.StringBuffer fileToStringBuffer(java.io.File file)
                                                 throws java.io.IOException
Retrieves an entire file and loads it into a StringBuffer.

Parameters:
file - the file to be loaded
Returns:
a buffer containing the text that was in the file
Throws:
java.io.IOException - if something went wrong opening or reading from the file

fileToStringBuffer

public static java.lang.StringBuffer fileToStringBuffer(java.lang.String fileName)
                                                 throws java.io.IOException
Retrieves an entire file and loads it into a StringBuffer.

Parameters:
fileName - the name of the file to be loaded
Returns:
a buffer containing the text that was in the file
Throws:
java.io.IOException - if something went wrong opening or reading from the file

fileToStringBuffer

public static java.lang.StringBuffer fileToStringBuffer(java.lang.String fileName,
                                                        java.lang.String encoding)
                                                 throws java.io.IOException
Retrieves an entire file and loads it into a StringBuffer. Loads with the specified encoding.

Parameters:
fileName - the name of the file to be loaded
encoding - the encoding to use when reading the file
Returns:
a buffer containing the text that was in the file
Throws:
java.io.IOException - if something went wrong opening or reading from the file

fileToStringBuffer

public static java.lang.StringBuffer fileToStringBuffer(java.io.File file,
                                                        java.lang.String encoding)
                                                 throws java.io.IOException
Retrieves an entire file and loads it into a StringBuffer. Loads with the specified encoding.
Note: Any buffer loaded from a file that does not end with a new line will have one added.

Parameters:
file - the file to be loaded
encoding - the encoding to use when reading the file
Returns:
a buffer containing the text that was in the file
Throws:
java.io.IOException - if something went wrong opening or reading from the file

bufferToFile

public static void bufferToFile(java.lang.StringBuffer sb,
                                java.lang.String fileName)
                         throws java.io.IOException
Saves the contents of a StringBuffer to a file, using the default encoding.

Parameters:
sb - the buffer to be saved
fileName - the name of the file that the buffer should be saved to
Throws:
java.io.IOException - if something went wrong opening or writing to the file

bufferToFile

public static void bufferToFile(java.lang.StringBuffer sb,
                                java.lang.String fileName,
                                java.lang.String encoding)
                         throws java.io.IOException
Saves the contents of a StringBuffer to a file, using the default encoding.

Parameters:
sb - the buffer to be saved
fileName - the name of the file that the buffer should be saved to
encoding - the encoding to use when reading the file
Throws:
java.io.IOException - if something went wrong opening or writing to the file

bufferToFile

public static void bufferToFile(java.lang.StringBuffer sb,
                                java.io.File file)
                         throws java.io.IOException
Saves the contents of a StringBuffer to a file, using the default encoding.

Parameters:
sb - the buffer to be saved
file - the file that the buffer should be saved to
Throws:
java.io.IOException - if something went wrong opening or writing to the file

bufferToFile

public static void bufferToFile(java.lang.StringBuffer sb,
                                java.io.File file,
                                java.lang.String encoding)
                         throws java.io.IOException
Saves the contents of a StringBuffer to a file, using the specified encoding.

Parameters:
sb - the buffer to be saved
file - the file that the buffer should be saved to
encoding - the encoding to use when writing the file
Throws:
java.io.IOException - if something went wrong opening or writing to the file

findSubDirectories

public static java.util.ArrayList<java.io.File> findSubDirectories(java.io.File root,
                                                                   int level,
                                                                   boolean leavesOnly)
Recursivly finds all of the subdirectories of a given root.

Parameters:
root - the root directory from which to begin searching (need not be an actual root dir.
level - The level of depth to transverse until
leavesOnly - flag indicating whether to only return the leaves (those dirs at the level of depth or all of the parent dirs as well
Returns:
an arraylist of the sub dirs

getNewestLastModified

public static java.io.File getNewestLastModified(java.io.File dir)
Finds the most recently modified file in the given directory

Parameters:
dir - the directory to search in
Returns:
the most recently modified file or null if the dir is not a dir or is empty

findSubDir

public static java.io.File findSubDir(java.lang.String dirname)