org.apache.uima.pear.util
Class FileUtil

java.lang.Object
  extended by org.apache.uima.pear.util.FileUtil

public class FileUtil
extends Object

The FileUtil class provides utility methods for working with general files.


Nested Class Summary
static class FileUtil.DirFileFilter
          The DirFileFilter class allows to filter files based on specified directory path and filename extension.
static class FileUtil.ExtFilenameFilter
          The ExtFileFilter allows to filter file names based on the specified filename extension.
static class FileUtil.FileTimeComparator
          The FileTimeComparator class allows comparing 'last modified' time in 2 given File objects.
static class FileUtil.NameFileFilter
          The NameFileFilter class allows to filter files based on specified file name.
 
Constructor Summary
FileUtil()
           
 
Method Summary
static int cleanUpDirectoryContent(File directory)
          Deletes all files and subdirectories in a given directory.
static int cleanUpDirectoryFiles(File directory)
          Deletes all files in a given directory.
static int cleanUpDirectoryFiles(File directory, int maxLimit)
          Cleans-up a given directory by keeping the number of files within a given limit.
static String computeRelativePath(File referenceDir, File file)
          Computes relative path to a given file from a given reference directory, if both the reference directory and the file are in the same logical file system (partition).
static boolean copyFile(File source, File destination)
          Copies the content of a given source file to a given destination file.
static boolean copyFile(URL sourceUrl, File destination)
          Copies the content of a given remote source file to a given destination file.
static Collection<File> createDirList(File rootDir)
          Creates list of subdirectories in a given root directory, including all its subdirectories.
static Collection<File> createDirList(File rootDir, boolean includeSubdirs)
          Creates list of subdirectories in a given root directory.
static Collection<File> createDirList(JarFile archive)
          Creates a list of directories in a given archive (JAR) file.
static Collection<File> createFileList(File filesDir)
          Creates list of files in a given directory, including all its subdirectories.
static Collection<File> createFileList(File filesDir, boolean includeSubdirs)
          Creates list of files in a given directory.
static Collection<File> createFileList(JarFile archive)
          Creates a list of files in a given archive (JAR) file.
static File createTempFile(String prefix, String suffix)
          Creates a new empty file in a directory specified by the 'java.io.tmpdir' or the 'user.home' system property, using given prefix and suffix strings to generate its name.
static boolean deleteDirectory(File dir)
          Deletes a given directory, including all its subdirectories and files.
static long extractDirectoryFromJar(JarFile jarFile, String dirPath, File targetDir)
          Extracts all files in a given JAR directory (including all its subdirectories) from a given JAR file to a given target directory.
static long extractFilesFromJar(JarFile jarFile, File targetDir)
          Extracts all files from a given JAR file to a given target directory.
static long extractFilesFromJar(JarFile jarFile, File targetDir, FileFilter filter)
          Extracts files from a given JAR file to a given target directory, based on a given FileFilter object.
static long extractFilesWithExtFromJar(JarFile jarFile, String fileExt, File targetDir)
          Extracts all files that have a given extension from a given JAR file to a given target directory.
static String getAbsolutePath(File rootDir, String relativePath)
          Constructs an absolute path of a given object, located in a given root directory, based on its relative path in this directory.
static String getFileNameExtension(String fileName)
          Identifies a given file name extension.
static long getFileSize(String fileLocation)
          Returns file size for a given file.
static String getRelativePath(File rootDir, String absolutePath)
          Constructs a relative path of a given object, located in a given root directory, based on its absolute path.
static String identifyUtfSignature(int[] prefix, int length)
          Makes and attempt to identify possible UTF signatute (BOM) in a given sequence of bytes.
static boolean isAsciiFile(File textFile)
          Returns true, if a given text file contains only ASCII characters, otherwise returns false.
static boolean isAsciiStream(InputStream iStream)
          Returns true, if a given input stream contains only ASCII characters, otherwise returns false.
static String[] loadListOfStrings(BufferedReader iStream)
          Loads a list of non-empty EOL-delimited strings from a given text stream.
static String[] loadListOfStrings(File textFile)
          Loads a list of non-empty EOL-delimited strings from a given text file using the default file encoding.
static String[] loadListOfStrings(URL textFileURL)
          Loads a list of non-empty EOL-delimited strings from a given remote text file.
static Properties loadPropertiesFromJar(String propFilePath, JarFile jarFile)
          Loads a specified properties file from a given JAR file.
static String loadTextFile(BufferedReader iStream)
          Loads a text file associated with a given input stream.
static String loadTextFile(File textFile)
          Loads a given local text file using the default file encoding.
static String loadTextFile(File textFile, String encoding)
          Loads a given local text file using a specified file encoding.
static String loadTextFile(URL textFileURL)
          Loads a given remote text file.
static String loadTextFile(URLConnection urlConnection)
          Loads a given remote text file.
static String loadTextFileFromJar(String filePath, JarFile jarFile)
          Loads a specified text file from a given JAR file.
static String localPathToFileUrl(String path)
          Converts a given input file path into a valid file URL string.
static boolean moveFile(File source, File destinationDir)
          Moves a given source file to a given destination directory.
static int replaceStringInFile(File textFile, String subStringRegex, String replacement)
          Replaces all occurences of a given regular expression with a given string in a given text file.
static SortedSet<File> sortFileListByTime(Collection<File> fileList)
          Sorts a given list of files by the 'last modified' time in the descending order.
static File zipDirectory(File dir2zip)
          Zips the contents of a given directory.
static File zipDirectory(File dir2zip, File zippedFile)
          Zips the contents of a given directory to a given output ZIP file.
static ZipOutputStream zipDirectory(File dir2zip, ZipOutputStream zoStream, File referenceDir, File[] excludeFiles)
          Zips the contents of a given directory to a given ZIP output stream.
static File zipFile(File file2zip)
          Zips a given file.
static File zipFile(File file2zip, File zippedFile)
          Zips a given file to a given output ZIP file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

cleanUpDirectoryContent

public static int cleanUpDirectoryContent(File directory)
                                   throws IOException
Deletes all files and subdirectories in a given directory. In case of unsuccessful deletion, calls the deleteOnExit() method to request that files and subdirs are deleted when the JVM terminates.

Parameters:
directory - The given directory to be cleaned-up.
Returns:
The number of successfully deleted entries in the given directory.
Throws:
IOException - If an I/O exception occurred.

cleanUpDirectoryFiles

public static int cleanUpDirectoryFiles(File directory)
                                 throws IOException
Deletes all files in a given directory. In case of unsuccessful deletion, calls the deleteOnExit() method to request that files are deleted when the JVM terminates.

Parameters:
directory - The given directory to be cleaned-up.
Returns:
The number of successfully deleted entries in the given directory.
Throws:
IOException - If an I/O exception occurred.

cleanUpDirectoryFiles

public static int cleanUpDirectoryFiles(File directory,
                                        int maxLimit)
                                 throws IOException
Cleans-up a given directory by keeping the number of files within a given limit. Deletes the oldest files first. In case of unsuccessful deletion, calls the deleteOnExit() method to request that files are deleted when the JVM terminates.

Parameters:
directory - The given directory.
maxLimit - The given maximum limit of the number of files in the given directory.
Returns:
The number of actually deleted files.
Throws:
IOException - If an I/O exception occurred.

computeRelativePath

public static String computeRelativePath(File referenceDir,
                                         File file)
                                  throws IOException
Computes relative path to a given file from a given reference directory, if both the reference directory and the file are in the same logical file system (partition).

Parameters:
referenceDir - The given reference directory.
file - The given file.
Returns:
The relative path to the given file from the given reference directory, or null, if the relative path does not exist.
Throws:
IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries.

copyFile

public static boolean copyFile(File source,
                               File destination)
                        throws IOException
Copies the content of a given source file to a given destination file.

Parameters:
source - The given source file.
destination - The given destination file.
Returns:
true if the copy operation completed successfully, false otherwise.
Throws:
IOException - If any I/O exception occurred.

copyFile

public static boolean copyFile(URL sourceUrl,
                               File destination)
                        throws IOException
Copies the content of a given remote source file to a given destination file.

Parameters:
sourceUrl - The given source file URL.
destination - The given destination file.
Returns:
true if the copy operation completed successfully, false otherwise.
Throws:
IOException - If any I/O exception occurred.

createDirList

public static Collection<File> createDirList(File rootDir)
                                      throws IOException
Creates list of subdirectories in a given root directory, including all its subdirectories.

Parameters:
rootDir - The given root directory.
Returns:
Collection of File objects, representing subdirectories in the given root directory and all its subdirectories.
Throws:
IOException - If any I/O exception occurs.

createDirList

public static Collection<File> createDirList(File rootDir,
                                             boolean includeSubdirs)
                                      throws IOException
Creates list of subdirectories in a given root directory. If a given boolean flag is true, all the subdirectories of the given root directory are also scanned, otherwise only subdirectories in the given root directory are included.

Parameters:
rootDir - The given root directory.
includeSubdirs - If true, the returned list includes sub-directories from all sub-directories of the given root directory, otherwise it includes only sub-directories from the given root directory itself.
Returns:
Collection of File objects, representing subdirectories in the given root directory.
Throws:
IOException - If any I/O exception occurs.

createDirList

public static Collection<File> createDirList(JarFile archive)
                                      throws IOException
Creates a list of directories in a given archive (JAR) file. The root directory path, used to represent the directories, is set to the input archive file path without the file name extension.

Parameters:
archive - The input archive (JAR) file.
Returns:
Collection of File objects, representing directories in the given archive file.
Throws:
IOException - If any I/O exception occurs.

createFileList

public static Collection<File> createFileList(File filesDir)
                                       throws IOException
Creates list of files in a given directory, including all its subdirectories.

Parameters:
filesDir - The given directory.
Returns:
Collection of File objects in the given directory, including all its subdirectories.
Throws:
IOException - If any I/O exception occurs.

createFileList

public static Collection<File> createFileList(File filesDir,
                                              boolean includeSubdirs)
                                       throws IOException
Creates list of files in a given directory. If a given boolean flag is true, all the sub-directories of the given directory are also scanned, otherwise only files in the given directory are included.

Parameters:
filesDir - The given directory.
includeSubdirs - If true, the returned file list includes files from all the sub-directories of the given directory, otherwise it includes only files from the given directory itself.
Returns:
Collection of File objects in the given directory.
Throws:
IOException - If any I/O exception occurs.

createFileList

public static Collection<File> createFileList(JarFile archive)
                                       throws IOException
Creates a list of files in a given archive (JAR) file. The root directory path, used to represent the files, is set to the input archive file path without the file name extension.

Parameters:
archive - The input archive (JAR) file.
Returns:
Collection of File objects, representing files in the given archive file.
Throws:
IOException - If any I/O exception occurs.

createTempFile

public static File createTempFile(String prefix,
                                  String suffix)
                           throws IOException
Creates a new empty file in a directory specified by the 'java.io.tmpdir' or the 'user.home' system property, using given prefix and suffix strings to generate its name. For more information see the documentation on the java.io.File.createTempFile() method.

Parameters:
prefix - The given prefix string to be used in generating the file's name; must be at least three characters long.
suffix - The given suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used.
Returns:
The File object denoting the newly created file.
Throws:
IOException - If a temporary directory not found or other I/O exception occurred.

deleteDirectory

public static boolean deleteDirectory(File dir)
                               throws IOException
Deletes a given directory, including all its subdirectories and files. Returns true if the deletion was successful, otherwise returns false. In case of unsuccessful deletion, calls deleteOnExit() method to request that files and subdirs be deleted when the virtual machine terminates.

Parameters:
dir - The given directory to be deleted.
Returns:
true if the deletion was successful, otherwise false.
Throws:
IOException - If any I/O exception occurs.

extractDirectoryFromJar

public static long extractDirectoryFromJar(JarFile jarFile,
                                           String dirPath,
                                           File targetDir)
                                    throws IOException
Extracts all files in a given JAR directory (including all its subdirectories) from a given JAR file to a given target directory.

Parameters:
jarFile - The given JAR file.
dirPath - The given JAR directory.
targetDir - The given target directory.
Returns:
Total number of bytes extracted.
Throws:
IOException - If any I/O exception occurred.

extractFilesWithExtFromJar

public static long extractFilesWithExtFromJar(JarFile jarFile,
                                              String fileExt,
                                              File targetDir)
                                       throws IOException
Extracts all files that have a given extension from a given JAR file to a given target directory. To extract files without extension, use null as the fileExt parameter.

Parameters:
jarFile - The given JAR file.
fileExt - The given file extension.
targetDir - The given target directory.
Returns:
Total number of bytes extracted.
Throws:
IOException - If any I/O exception occurred.

extractFilesFromJar

public static long extractFilesFromJar(JarFile jarFile,
                                       File targetDir)
                                throws IOException
Extracts all files from a given JAR file to a given target directory.

Parameters:
jarFile - The given JAR file.
targetDir - The given target directory.
Returns:
Total number of bytes extracted.
Throws:
IOException - If any I/O exception occurred.

extractFilesFromJar

public static long extractFilesFromJar(JarFile jarFile,
                                       File targetDir,
                                       FileFilter filter)
                                throws IOException
Extracts files from a given JAR file to a given target directory, based on a given FileFilter object.

Parameters:
jarFile - The given JAR file.
targetDir - The given target directory.
filter - The given FileFilter object.
Returns:
Total number of bytes extracted.
Throws:
IOException - If any I/O exception occurred.

getAbsolutePath

public static String getAbsolutePath(File rootDir,
                                     String relativePath)
Constructs an absolute path of a given object, located in a given root directory, based on its relative path in this directory.

Parameters:
rootDir - The given root directory.
relativePath - The given relative path of the object.
Returns:
The absolute path for the given object, located in the given root directory.

getFileNameExtension

public static String getFileNameExtension(String fileName)
Identifies a given file name extension.

Parameters:
fileName - The given file name.
Returns:
The file name extension

getFileSize

public static long getFileSize(String fileLocation)
Returns file size for a given file.

Parameters:
fileLocation - The given file location - local file path or URL.
Returns:
The given file size, if the specified file can be accessed, -1 otherwise.

getRelativePath

public static String getRelativePath(File rootDir,
                                     String absolutePath)
Constructs a relative path of a given object, located in a given root directory, based on its absolute path.

Parameters:
rootDir - The given root directory.
absolutePath - The given absolute path of the object.
Returns:
The relative path of the given object, located in the given root directory.

identifyUtfSignature

public static String identifyUtfSignature(int[] prefix,
                                          int length)
Makes and attempt to identify possible UTF signatute (BOM) in a given sequence of bytes. Returns the identified UTF signature name or null, if the signature could not be identified. For more on UTF and its signatures see FAQ - UTF and BOM.

Parameters:
prefix - The given sequence of bytes to analyze.
length - The length of the given sequence of bytes.
Returns:
The UTF signature name or null, if the signature could not be identified.

isAsciiFile

public static boolean isAsciiFile(File textFile)
                           throws IOException
Returns true, if a given text file contains only ASCII characters, otherwise returns false.

Parameters:
textFile - The given text file.
Returns:
true, if the given text file contains only ASCII characters, false otherwise.
Throws:
IOException - If an I/O exception occurred.

isAsciiStream

public static boolean isAsciiStream(InputStream iStream)
                             throws IOException
Returns true, if a given input stream contains only ASCII characters, otherwise returns false.

Parameters:
iStream - The given input stream.
Returns:
true, if the given input stream contains only ASCII characters, false otherwise.
Throws:
IOException - If an I/O exception occurred.

loadListOfStrings

public static String[] loadListOfStrings(BufferedReader iStream)
                                  throws IOException
Loads a list of non-empty EOL-delimited strings from a given text stream.

Parameters:
iStream - The given input text stream.
Returns:
The array of non-empty strings loaded from the given text stream.
Throws:
IOException - If any I/O exception occurred.

loadListOfStrings

public static String[] loadListOfStrings(File textFile)
                                  throws IOException
Loads a list of non-empty EOL-delimited strings from a given text file using the default file encoding.

Parameters:
textFile - The given text file.
Returns:
The array of non-empty strings loaded from the given text file.
Throws:
IOException - If any I/O exception occurred.

loadListOfStrings

public static String[] loadListOfStrings(URL textFileURL)
                                  throws IOException
Loads a list of non-empty EOL-delimited strings from a given remote text file.

Parameters:
textFileURL - The URL of the given input text file.
Returns:
The array of non-empty strings loaded from the given text file.
Throws:
IOException - If any I/O exception occurred.

loadPropertiesFromJar

public static Properties loadPropertiesFromJar(String propFilePath,
                                               JarFile jarFile)
                                        throws IOException
Loads a specified properties file from a given JAR file.

Parameters:
propFilePath - The given properties file path in the JAR file.
jarFile - The given JAR file.
Returns:
Properties object containing loaded properties, or null, if the properties file was not found in the given JAR file.
Throws:
IOException - If any I/O exception occurred.

loadTextFile

public static String loadTextFile(BufferedReader iStream)
                           throws IOException
Loads a text file associated with a given input stream.

Parameters:
iStream - The given text input stream.
Returns:
The content of the text file.
Throws:
IOException - If any I/O exception occurs.

loadTextFile

public static String loadTextFile(File textFile)
                           throws IOException
Loads a given local text file using the default file encoding.

Parameters:
textFile - The given text file.
Returns:
The content of the text file.
Throws:
IOException - If any I/O exception occurs.

loadTextFile

public static String loadTextFile(File textFile,
                                  String encoding)
                           throws IOException
Loads a given local text file using a specified file encoding.

Parameters:
textFile - The given text file.
encoding - The given text file encoding name.
Returns:
The content of the text file.
Throws:
IOException - If any I/O exception occurs.

loadTextFile

public static String loadTextFile(URL textFileURL)
                           throws IOException
Loads a given remote text file.

Parameters:
textFileURL - The given text file URL.
Returns:
The content of the text file.
Throws:
IOException - If any I/O exception occurs.

loadTextFile

public static String loadTextFile(URLConnection urlConnection)
                           throws IOException
Loads a given remote text file.

Parameters:
urlConnection - The given URL connection.
Returns:
The content of the text file.
Throws:
IOException - If any I/O exception occurs.

loadTextFileFromJar

public static String loadTextFileFromJar(String filePath,
                                         JarFile jarFile)
                                  throws IOException
Loads a specified text file from a given JAR file.

Parameters:
filePath - The specified text file path inside the JAR file.
jarFile - The given JAR file.
Returns:
The content of the text specified file, or null, if the text file was not found in the given JAR file.
Throws:
IOException - If any I/O exception occurs.

localPathToFileUrl

public static String localPathToFileUrl(String path)
Converts a given input file path into a valid file URL string.

Parameters:
path - The given file path to be converted.
Returns:
The file URL string for the specified file.

moveFile

public static boolean moveFile(File source,
                               File destinationDir)
                        throws IOException
Moves a given source file to a given destination directory.

Parameters:
source - The given source file.
destinationDir - The given destination directory.
Returns:
true if the move operation completed successfully, false otherwise.
Throws:
IOException - If any I/O exception occurred.

replaceStringInFile

public static int replaceStringInFile(File textFile,
                                      String subStringRegex,
                                      String replacement)
                               throws IOException
Replaces all occurences of a given regular expression with a given string in a given text file. Supports only 1 file encoding - ASCII - for all general text files. Supports 2 encodings - UTF-8 (ASCII) and UTF-16 for XML files.

Parameters:
textFile - The given text file.
subStringRegex - The given regular expression string to be replaced.
replacement - The given replacement string.
Returns:
The number of actual string replacements performed.
Throws:
IOException - If any I/O exception occurs.

sortFileListByTime

public static SortedSet<File> sortFileListByTime(Collection<File> fileList)
Sorts a given list of files by the 'last modified' time in the descending order.

Parameters:
fileList - The given list of files.
Returns:
The list of files sorted by the 'last modified' time in the descending order.

zipDirectory

public static File zipDirectory(File dir2zip)
                         throws IOException
Zips the contents of a given directory. The output ZIP file, by default, is created in the given directory, and its name is the given directory name with 'zip' extension.

Parameters:
dir2zip - The given directory to be zipped.
Returns:
The output ZIP file.
Throws:
IOException - If any I/O exception occurred.

zipDirectory

public static File zipDirectory(File dir2zip,
                                File zippedFile)
                         throws IOException
Zips the contents of a given directory to a given output ZIP file.

Parameters:
dir2zip - The given directory to be zipped.
zippedFile - The given output ZIP file.
Returns:
The output ZIP file.
Throws:
IOException - If any I/O exception occurred.

zipDirectory

public static ZipOutputStream zipDirectory(File dir2zip,
                                           ZipOutputStream zoStream,
                                           File referenceDir,
                                           File[] excludeFiles)
                                    throws IOException
Zips the contents of a given directory to a given ZIP output stream. Paths of file entries in the ZIP stream are taken relatively to a given reference directory. If the reference directory is null, the file paths are taken relatively to the given directory to be zipped. The method allows to specify the list of files (or dirs) that should not be zipped.

Parameters:
dir2zip - The given directory to be zipped.
zoStream - The given ZIP output stream.
referenceDir - The given reference directory or null.
excludeFiles - The given list of files (or dirs) that should not be zipped.
Returns:
The ZIP output stream.
Throws:
IOException - If any I/O exception occurred.

zipFile

public static File zipFile(File file2zip)
                    throws IOException
Zips a given file. The output ZIP file, by default, is created in the same directory, as the given input file, and has the same name, as the given input file with 'zip' extension.

Parameters:
file2zip - The file to be zipped.
Returns:
The output ZIP file.
Throws:
IOException - If any I/O exception occurred.

zipFile

public static File zipFile(File file2zip,
                           File zippedFile)
                    throws IOException
Zips a given file to a given output ZIP file.

Parameters:
file2zip - The file to be zipped.
zippedFile - The given output ZIP file.
Returns:
The output ZIP file.
Throws:
IOException - If any I/O exception occurred.


Copyright © 2010 The Apache Software Foundation. All Rights Reserved.