mckay.utilities.staticlibraries
Class FileMethods

java.lang.Object
  extended by mckay.utilities.staticlibraries.FileMethods

public class FileMethods
extends java.lang.Object

A holder class for static methods relating to files.


Constructor Summary
FileMethods()
           
 
Method Summary
static void copyFile(java.lang.String original, java.lang.String destination)
          Copies the contents of one file to another.
static boolean createEmptyDirectory(java.lang.String path)
          Creates a directory at the given path.
static boolean deleteDirectoryRecursively(java.io.File directory)
          Deletes all files and sub-directories in the given directory.
static java.io.File[] getAllFilesInDirectory(java.io.File directory, boolean explore_subdirectories, java.io.FileFilter filter, java.util.Vector<java.io.File> results)
          Returns all files meeting the requirements of the given filter in the given directory and, if requested, its subdirectories.
static java.io.DataOutputStream getDataOutputStream(java.io.File file)
          Prepares a DataOutputStream that can be used to write a file.
static java.io.File getNewFileForWriting(java.lang.String path, boolean can_erase)
          Gets a new File object to write to based on the given path.
static java.lang.String[] parseTextFileLines(java.io.File to_parse)
          Parses the given text file.
static boolean validateFile(java.io.File file, boolean need_read, boolean need_write)
          Tests the given file to see if it is a valid file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileMethods

public FileMethods()
Method Detail

getNewFileForWriting

public static java.io.File getNewFileForWriting(java.lang.String path,
                                                boolean can_erase)
Gets a new File object to write to based on the given path. If the can_erase parameter is false, then the user is given a warning message through the GUI asking him/er if s/he wishes to overwrite the file. Returns null if the choice is to not overwrite file. Attempts to write an empty string and displays an error message if this cannot be done (also returns null in this case).

Parameters:
path - The path to which the file is to be saved.
can_erase - Whether or not the file should be automatically overwritten if it already exists.
Returns:
Returns the requested file, or null if a fie cannot be written to.

getDataOutputStream

public static java.io.DataOutputStream getDataOutputStream(java.io.File file)
                                                    throws java.io.FileNotFoundException
Prepares a DataOutputStream that can be used to write a file. The useer must remember to close this DataOutputStream after writing is complete.

Parameters:
file - The File that will be written to.
Returns:
A new DataOutputStream for the given file.
Throws:
java.io.FileNotFoundException - This exception is thrown if the given file cannot be found.

validateFile

public static boolean validateFile(java.io.File file,
                                   boolean need_read,
                                   boolean need_write)
                            throws java.lang.Exception
Tests the given file to see if it is a valid file.

Parameters:
file - The file to test.
need_read - If this is set to true, then an exception is sent if the file cannot be read from.
need_write - If this is set to true, then an exception is sent if the file cannot be written to.
Returns:
Value of true if no problems occured during file validation.
Throws:
java.lang.Exception - An informative exception is thrown if there is a problem with the file.

getAllFilesInDirectory

public static java.io.File[] getAllFilesInDirectory(java.io.File directory,
                                                    boolean explore_subdirectories,
                                                    java.io.FileFilter filter,
                                                    java.util.Vector<java.io.File> results)
Returns all files meeting the requirements of the given filter in the given directory and, if requested, its subdirectories.

Parameters:
directory - The directory to explore.
explore_subdirectories - Whether or not the subdirectories of directory should be explored.
filter - A filter controlling what files are elligible to be returned. A value of null means all files found will be returned.
results - Used internally for recursive calls. Pass null when calling this method externally.
Returns:
An array containing all files meeting the requirements of the other parameters. Null is returned if no files can be found or if the given directory is not a valid directory.

createEmptyDirectory

public static boolean createEmptyDirectory(java.lang.String path)
Creates a directory at the given path. If a file already exists at the given path, it is deleted. All contents of a directory that already exists at the given path are deleted.

If a deletion fails, then the method stops attempting to delete and returns false.

Parameters:
path - The path at which a directory is to be created.
Returns:
True if the directory was succesfully created. False if the directory could not be created or a pre-existing item could not be deleted.

deleteDirectoryRecursively

public static boolean deleteDirectoryRecursively(java.io.File directory)
Deletes all files and sub-directories in the given directory. If the given directory is actually a file, then it is just deleted.

If a deletion fails, then the method stops attempting to delete and returns false.

Parameters:
directory - The directory to delete.
Returns:
True if the directory and its contents were succesfully deleted, false if a failure to delete occured.

copyFile

public static void copyFile(java.lang.String original,
                            java.lang.String destination)
                     throws java.lang.Exception
Copies the contents of one file to another. Throws an exception if the destination file already exists or if the original file does not exist.

Parameters:
original - The name of the file to be copied.
destination - The name of the file to be copied to.
Throws:
java.lang.Exception - An exception is thrown if a problem occurs during copying.

parseTextFileLines

public static java.lang.String[] parseTextFileLines(java.io.File to_parse)
                                             throws java.lang.Exception
Parses the given text file. The parsed file is considered to comprise a list. Each line is counted as a separate item in the list. Blank lines are treated as an item in the list consisting of "". An array of strings is returned with one entry for each item in the list (i.e. each line). This array is not sorted or otherwise processed, but no entries may be null. A descriptive exception is thrown if a problem occurs during paring.

Parameters:
to_parse - The file to parse.
Returns:
The parsed contents of the file.
Throws:
java.lang.Exception - An informative description of any problem that occurs during parsing.