|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--uk.org.ogsadai.common.files.FileUtilities
Provides utilities for reading, writing and deleting files. Keeps files locked whilst in use, but allows many threads to read from the same file concurrently.
Field Summary | |
private static int |
BUFFER_SIZE |
private static java.lang.String |
COPYRIGHT_NOTICE |
private static FileLock |
LOCK |
private static DAILogger |
LOG |
private static FileUtilities |
SINGLETON |
Constructor Summary | |
private |
FileUtilities() A private constructor for the Singleton instance. |
Method Summary | |
static void |
closeStreamFromFile(java.lang.String fileName, java.io.InputStream fileDataStream) Close an InputStream for a file. |
static void |
closeStreamToFile(java.lang.String fileName, java.io.OutputStream fileDataStream) Close an OutputStream for a file. |
static void |
copyDirectoryContents(java.io.File sourceDir, java.io.File destinationDir) Copies the contents of the source directory to the destination directory. |
static void |
copyFile(java.io.File sourceFile, java.io.File destinationFile) Copies the source file to the destination file. |
static boolean |
deleteDir(java.io.File dir) Deletes the given directory and all files and subdirectories below it. |
static boolean |
deleteDirContents(java.io.File dir) Deletes the contents of the directory including all subdirectories. |
static void |
deleteFile(java.lang.String fileName) Deletes a file. |
static void |
directoryDoesntExist(java.io.File dir) A utility method to test whether a directory does not exist. |
static void |
directoryExists(java.io.File dir) A utility method to test whether a directory exists. |
static void |
fileDoesntExist(java.io.File file) A utility method to test whether a file does not exist. |
static void |
fileExists(java.io.File file) A utility method to test whether a file exists. |
static java.io.File |
getFile(java.lang.String baseDir, java.lang.String filename) Gets the file specified by the given filename and possibly also the given base directory. |
static FileUtilities |
getInstance() Gets the singleton instance of this class. |
static void |
isDirectory(java.io.File file) A utility method to test whether a given File represents a directory. |
static void |
isDirectoryReadable(java.io.File dir) A utility method to test whether a directory is readable. |
static void |
isFileReadable(java.io.File file) A utility method to test whether a file is readable. |
static void |
isFileWritable(java.io.File file) A utility method to test whether a file is writable. |
static java.io.InputStream |
openStreamFromFile(java.lang.String fileName) Open an InputStream for a file. |
static java.io.OutputStream |
openStreamToFile(java.lang.String fileName) Open an OutputStream for a new file. |
static byte[] |
readByteArrayFromFile(java.lang.String fileName) Reads the contents of a file into a byte array. |
static java.lang.String |
readStringFromFile(java.lang.String fileName) Reads the contents of a file into a string. |
static java.lang.String |
readStringFromStream(java.io.InputStream inputStream) Reads data from a given stream and returns it as a string. |
static void |
renameFile(java.lang.String fileName, java.lang.String newFileName) Renames a file. |
static void |
replaceTokensInDirectory(java.io.File dir, java.util.Map tokensAndValues) Replaces all occurrences of token regular expressions in each file in the specified directory with the corresponding value strings. |
static void |
replaceTokensInFile(java.io.File file, java.util.Map tokensAndValues) Replaces all occurrences of token regular expressions in a file with the corresponding value strings. |
static void |
replaceWriteStringToFile(java.lang.String fileName, java.lang.String data) Writes the contents of a string to a file. |
static void |
writeByteArrayToFile(java.lang.String fileName, byte[] data) Writes the contents of a string to a byte array to a new file. |
static void |
writeStringToFile(java.lang.String fileName, java.lang.String data) Writes the contents of a string to a new file. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final java.lang.String COPYRIGHT_NOTICE
private static final DAILogger LOG
private static final FileUtilities SINGLETON
private static final FileLock LOCK
private static final int BUFFER_SIZE
Constructor Detail |
private FileUtilities()
Method Detail |
public static FileUtilities getInstance()
public static java.lang.String readStringFromFile(java.lang.String fileName) throws DAIFileNotFoundException, DAIFileNotFileException, DAIFileReadIOException
fileName
- Name of file to read.DAIFileNotFoundException
- if the file cannot be found.DAIFileNotFileException
- if the file is not a file e.g. it's a directory.DAIFileReadIOException
- if there is an IO error when reading the file.java.lang.IllegalArgumentException
- if fileName
is null
.public static void replaceWriteStringToFile(java.lang.String fileName, java.lang.String data) throws DAIFileNotFileException, DAIFileWriteIOException
fileName
- Name of file to write to.data
- The data to write to the file.DAIFileNotFileException
- if the specified file is a non-file, for example a directory.DAIFileWriteIOException
- if there is an error writing to the file.java.lang.IllegalArgumentException
- if fileName
is null
.public static void writeStringToFile(java.lang.String fileName, java.lang.String data) throws DAIFileNotFileException, DAIFileWriteIOException, DAIFileAlreadyExistsException
fileName
- Name of file to write to.data
- The data to write to the file.DAIFileAlreadyExistsException
- if the file already exists.DAIFileNotFileException
- if the specified file is a non-file, for example a directory.DAIFileWriteIOException
- if there is an error writing to the file.java.lang.IllegalArgumentException
- if fileName
is null
.public static byte[] readByteArrayFromFile(java.lang.String fileName) throws DAIFileNotFoundException, DAIFileNotFileException, DAIFileReadIOException
fileName
- Name of file to read.DAIFileNotFoundException
- if the file cannot be found.DAIFileNotFileException
- if the file is not a file e.g. it's a directory.DAIFileReadIOException
- if there is an IO error when reading the file.java.lang.IllegalArgumentException
- if fileName
is null
.public static void writeByteArrayToFile(java.lang.String fileName, byte[] data) throws DAIFileNotFileException, DAIFileWriteIOException, DAIFileAlreadyExistsException
fileName
- Name of file to write to.data
- The data to write to the file.DAIFileAlreadyExistsException
- if the file already exists.DAIFileNotFileException
- if the specified file is a non-file, for example a directory.DAIFileWriteIOException
- if there is an error writing to the file.java.lang.IllegalArgumentException
- if either argument is null
.public static java.io.InputStream openStreamFromFile(java.lang.String fileName) throws DAIFileNotFoundException, DAIFileNotFileException, DAIFileReadIOException
InputStream
for a file.
fileName
- Name of file.InputStream
.DAIFileNotFoundException
- if the file cannot be found.DAIFileNotFileException
- if the file is not a file e.g. it's a directory.DAIFileReadIOException
- if there is an IO error when reading the file.java.lang.IllegalArgumentException
- if fileName
is null
.public static void closeStreamFromFile(java.lang.String fileName, java.io.InputStream fileDataStream) throws DAIFileReadIOException
InputStream
for a file.
Assumes that the InputStream
was created using openStreamFromFile()
fileName
- Name of file.fileDataStream
- File InputStream
.DAIFileReadIOException
- if there is an IO error when reading the file.java.lang.IllegalArgumentException
- if either argument is null
.public static java.lang.String readStringFromStream(java.io.InputStream inputStream) throws java.io.IOException
inputStream
- Stream to read from.java.lang.IllegalArgumentException
- if inputStream
is null
java.io.IOException
- if there is an error reading from the stream.public static java.io.OutputStream openStreamToFile(java.lang.String fileName) throws DAIFileNotFileException, DAIFileWriteIOException
OutputStream
for a new file.
fileName
- Name of file.OutputStream
.DAIFileNotFileException
- if the file is not a file e.g. it's a directory.DAIFileWriteIOException
- if there is an IO error when writing the file.java.lang.IllegalArgumentException
- if fileName
is null
.public static void closeStreamToFile(java.lang.String fileName, java.io.OutputStream fileDataStream) throws DAIFileWriteIOException
OutputStream
for a file.
Assumes that the OutputStream
was created using openStreamToFile()
fileName
- Name of file.fileDataStream
- File OutputStream
.DAIFileWriteIOException
- if there is an IO error when reading the file.java.lang.IllegalArgumentException
- if either argument is null
.public static void renameFile(java.lang.String fileName, java.lang.String newFileName) throws DAIFileNotFileException, DAIFileWriteIOException, DAIFileRenameException, DAIFileAlreadyExistsException, DAIFileNotFoundException
fileName
- Name of file to rename.newFileName
- New file name.DAIFileNotFileException
- if the specified files are a non-file, for example a directory.DAIFileNotFoundException
- if the specified file is non-existant.DAIFileRenameException
- if the file just didn't rename.DAIFileWriteIOException
- if there is an error renaming the file.DAIFileAlreadyExistsException
- if the file already exists.java.lang.IllegalArgumentException
- if fileName
or newFileName
is null
.public static void deleteFile(java.lang.String fileName) throws DAIFileNotFileException, DAIFileWriteIOException, DAIFileDeleteException, DAIFileNotFoundException
fileName
- Name of file to delete.DAIFileDeleteException
- if the file just didn't delete.DAIFileNotFileException
- if the specified file is a non-file, for example a directory.DAIFileNotFoundException
- if the specified file does not exist.DAIFileWriteIOException
- if there is an error deleting the file.java.lang.IllegalArgumentException
- if fileName
is null
.public static java.io.File getFile(java.lang.String baseDir, java.lang.String filename)
baseDir
- Absolute path to base directory.filename
- Name of file.java.lang.IllegalArgumentException
- if either argument is null
.public static void replaceTokensInDirectory(java.io.File dir, java.util.Map tokensAndValues) throws DAIFileReadIOException, DAIFileWriteIOException, DAIDirectoryNotFoundException, DAIDirectoryNotDirectoryException
dir
- Directory to processtokensAndValues
- Mappings of token regular expressions to value strings. All occurences of each token regular expression within the file will be replaced by the corresponding value string.DAIDirectoryNotFoundException
- if the specified directory is not found.DAIDirectoryNotDirectoryException
- if the specified directory is not actually a directory.DAIFileReadIOException
- if there is an IO error reading a file.DAIFileWriteIOException
- if there is an IO error writing to a file.java.lang.IllegalArgumentException
- if dir
or tokensAndValues
are null
.public static void replaceTokensInFile(java.io.File file, java.util.Map tokensAndValues) throws DAIFileNotFoundException, DAIFileNotFileException, DAIFileReadIOException, DAIFileWriteIOException
file
- File to be altered.tokensAndValues
- Mappings of token strings to value strings. All occurences of each token regular expression within the file will be replaced by the corresponding value string.DAIFileNotFoundException
- if the file does not exist.DAIFileNotFileException
- if the specified file is not a file, for example it is a directory.DAIFileReadIOException
- if there is an IO error reading a file.DAIFileWriteIOException
- if there is an IO error writing to a file.java.lang.IllegalArgumentException
- if file
or tokensAndValues
are null
.public static boolean deleteDir(java.io.File dir)
dir
- Directory to deletetrue
if the delete is successfull, false
otherwise.java.lang.IllegalArgumentException
- if dir
is null
.public static boolean deleteDirContents(java.io.File dir)
dir
- Directory whose contents are to be deleted.true
if the delete is successfull, false
otherwise.java.lang.IllegalArgumentException
- if dir
is null
.public static void copyFile(java.io.File sourceFile, java.io.File destinationFile) throws DAIFileNotFoundException, DAIFileReadIOException, DAIFileWriteIOException, DAIFileNotFileException
sourceFile
- Source file.destinationFile
- Destination file.DAIFileNotFileException
- if the source file is not a file e.g. it's a directory.DAIFileNotFoundException
- if the source file cannot be found or the destination file cannot be reached.DAIFileReadIOException
- if there is an IO error when reading from to the source file.DAIFileWriteIOException
- if there is an IO error when writing to the destination file.java.lang.IllegalArgumentException
- if either argument is null
.public static void copyDirectoryContents(java.io.File sourceDir, java.io.File destinationDir) throws DAIDirectoryNotFoundException, DAIDirectoryNotDirectoryException, DAIFileReadIOException, DAIFileWriteIOException
sourceDir
- Source directory.destinationDir
- Destination directory.DAIDirectoryNotFoundException
- if either the source or destination directory cannot be found.DAIDirectoryNotDirectoryException
- if either the source or destination directory is not a directory.DAIFileWriteIOException
- if there is an IO error when writing a file.DAIFileReadIOException
- if there is an IO error when reading a file.java.lang.IllegalArgumentException
- if either argument is null
.public static void fileExists(java.io.File file) throws DAIFileNotFoundException
file
- File to test.DAIFileNotFoundException
- if the file does not exist.public static void fileDoesntExist(java.io.File file) throws DAIFileFoundException
file
- File to test.DAIFileFoundException
- if the file exists.public static void directoryExists(java.io.File dir) throws DAIDirectoryNotFoundException
dir
- Directory to test.DAIDirectoryNotFoundException
- if the directory does not exist.public static void directoryDoesntExist(java.io.File dir) throws DAIDirectoryFoundException
dir
- Directory to test.DAIDirectoryFoundException
- if the directory exists.public static void isDirectory(java.io.File file) throws DAIDirectoryNotDirectoryException
File
represents a directory.
file
- File to test.DAIDirectoryNotDirectoryException
- if file
does not represent a directory.public static void isFileReadable(java.io.File file) throws DAIFileReadIOException
file
- File to test.DAIFileReadIOException
- if the file is not readable.public static void isFileWritable(java.io.File file) throws DAIFileWriteIOException
file
- File to test.DAIFileWriteIOException
- if the file is not writable.public static void isDirectoryReadable(java.io.File dir) throws DAIDirectoryReadException
dir
- Directory to test.DAIDirectoryReadException
- if the file is not readable.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |