org.aitools.programd.util
Class FileManager

java.lang.Object
  extended by org.aitools.programd.util.FileManager

public class FileManager
extends Object

FileManager provides a standard interface for getting File objects and paths.


Field Summary
static String FILE
          The string ""file"".
 
Constructor Summary
FileManager()
           
 
Method Summary
static File checkOrCreate(String path, String description)
          Checks whether a file given by a path exists, and if not, creates it, along with any necessary subdirectories.
static File checkOrCreateDirectory(String path, String description)
          Checks whether a directory given by a path exists, and if not, creates it, along with any necessary subdirectories.
static String getAbsolutePath(String path)
          Returns the absolute path.
static File getBestFile(String path)
          Gets a file from a given path.
static File getExistingDirectory(String path)
          Sames as getExistingFile(java.lang.String) except that it also checks that the given path is a directory.
static File getExistingFile(String path)
          Gets a file from a given path.
static String getFileContents(String path)
          Returns the entire contents of a file as a String.
static FileInputStream getFileInputStream(String path)
          Opens and returns a FileInputStream for a given path.
static FileOutputStream getFileOutputStream(String path)
          Opens and returns a FileOutputStream for a given path.
static FileWriter getFileWriter(String path, boolean append)
          Gets a FileWriter from a given path.
static URL getRootPath()
          Returns the root path.
static URL getWorkingDirectory()
          Returns the working directory.
static List<File> glob(String path)
          Expands a localized file name that may contain wildcards to an array of file names without wildcards.
static List<File> glob(String path, String workingDirectoryToUse)
           Expands a localized file name that may contain wildcards to an array of file names without wildcards.
static String loadFileAsString(File file)
          Loads a file into a String.
static String loadFileAsString(String path)
          Loads a file into a String.
static void popWorkingDirectory()
          Pops a working directory off the stack.
static void pushWorkingDirectory(URL path)
          Pushes a new working directory onto the stack.
static void setRootPath(URL url)
          Sets the root path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE

public static final String FILE
The string ""file"".

See Also:
Constant Field Values
Constructor Detail

FileManager

public FileManager()
Method Detail

setRootPath

public static void setRootPath(URL url)
Sets the root path.

Parameters:
url - the root path

getRootPath

public static URL getRootPath()
Returns the root path.

Returns:
the root path

getBestFile

public static File getBestFile(String path)
Gets a file from a given path. Tries to return it as a canonical file; failing that, returns the absolute file, (which may not be valid, but we do not check that here).

Parameters:
path - the path for the file
Returns:
the file (may not exist!)

getExistingFile

public static File getExistingFile(String path)
                            throws FileNotFoundException
Gets a file from a given path. First tries to use the path as-is if it's absolute, then looks in the current working directory. The file must already exist, or an exception will be thrown.

Parameters:
path - the path for the file (may be absolute or relative to root directory)
Returns:
the file
Throws:
FileNotFoundException

getExistingDirectory

public static File getExistingDirectory(String path)
Sames as getExistingFile(java.lang.String) except that it also checks that the given path is a directory.

Parameters:
path - the path for the directory (may be absolute or relative to root directory)
Returns:
the directory

getFileInputStream

public static FileInputStream getFileInputStream(String path)
                                          throws FileNotFoundException
Opens and returns a FileInputStream for a given path. If the specified file doesn't exist, an exception will be thrown.

Parameters:
path -
Returns:
a stream pointing to the given path
Throws:
FileNotFoundException - if the file does not exist

getFileOutputStream

public static FileOutputStream getFileOutputStream(String path)
                                            throws FileNotFoundException
Opens and returns a FileOutputStream for a given path. If the specified file doesn't exist, an exception will be thrown.

Parameters:
path - the path to which to return a stream
Returns:
a stream pointing to the given path
Throws:
FileNotFoundException - if the file does not exist

getFileWriter

public static FileWriter getFileWriter(String path,
                                       boolean append)
                                throws IOException
Gets a FileWriter from a given path. First tries to use the path as-is if it's absolute, then (otherwise) looks in the defined root directory.

Parameters:
path - the path for the file (may be absolute or relative to root directory)
append - if true, then bytes will be written to the end of the file rather than the beginning
Returns:
the FileWriter
Throws:
IOException - if the specified file is not found or if some other I/O error occurs

getAbsolutePath

public static String getAbsolutePath(String path)
                              throws FileNotFoundException
Returns the absolute path. First checks whether the path as-is is absolute, then (otherwise) looks in the defined root directory.

Parameters:
path - the path for the file (may be absolute or relative to root directory)
Returns:
the absolute path
Throws:
FileNotFoundException - if a file with the given path cannot be located

checkOrCreate

public static File checkOrCreate(String path,
                                 String description)
Checks whether a file given by a path exists, and if not, creates it, along with any necessary subdirectories.

Parameters:
path - denoting the file to create
description - describes what the file is for, for trace messages. Should fit into a sentence like, "created new description ". May be null (which will result in less informative messages).
Returns:
the file that is created (or retrieved)

checkOrCreateDirectory

public static File checkOrCreateDirectory(String path,
                                          String description)
Checks whether a directory given by a path exists, and if not, creates it, along with any necessary subdirectories.

Parameters:
path - denoting the directory to create
description - describes what the directory is for, for trace messages. Should fit into a sentence like, "created new description ". May be null (which will result in less informative messages).
Returns:
the directory that is created (or retrieved)

getFileContents

public static String getFileContents(String path)
Returns the entire contents of a file as a String.

Parameters:
path - the path to the file (local file or URL)
Returns:
the entire contents of a file as a String

glob

public static List<File> glob(String path)
                       throws FileNotFoundException
Expands a localized file name that may contain wildcards to an array of file names without wildcards. All file separators in the file name must preceed any wildcard. The current directory is assumed to be the working directory.

Parameters:
path -
Returns:
array of file names without wildcards
Throws:
FileNotFoundException - if wild card is misused

glob

public static List<File> glob(String path,
                              String workingDirectoryToUse)
                       throws FileNotFoundException

Expands a localized file name that may contain wildcards to an array of file names without wildcards. All file separators in the file name must preceed any wildcard.

Adapted, with gratitude, from the JMK project. (Under the GNU LGPL)

Parameters:
path - the path string to glob
workingDirectoryToUse - the path to which relative paths should be considered relative
Returns:
array of file names without wildcards
Throws:
FileNotFoundException - if wild card is misused
See Also:
JMK

pushWorkingDirectory

public static void pushWorkingDirectory(URL path)
Pushes a new working directory onto the stack.

Parameters:
path - the directory path

popWorkingDirectory

public static void popWorkingDirectory()
Pops a working directory off the stack.


getWorkingDirectory

public static URL getWorkingDirectory()
Returns the working directory.

Returns:
the working directory

loadFileAsString

public static String loadFileAsString(String path)
                               throws FileNotFoundException
Loads a file into a String.

Parameters:
path - the path to the file
Returns:
the loaded template
Throws:
FileNotFoundException

loadFileAsString

public static String loadFileAsString(File file)
Loads a file into a String.

Parameters:
file - the file
Returns:
the loaded template