Package org.apache.uima.pear.util
Class FileUtil
java.lang.Object
org.apache.uima.pear.util.FileUtil
The
FileUtil class provides utility methods for working with general files.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTheDirFileFilterclass allows to filter files based on specified directory path and filename extension.static classTheExtFileFilterallows to filter file names based on the specified filename extension.static classTheFileTimeComparatorclass allows comparing 'last modified' time in 2 givenFileobjects.static classTheNameFileFilterclass allows to filter files based on specified file name. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intcleanUpDirectoryContent(File directory) Deletes all files and subdirectories in a given directory.static intcleanUpDirectoryFiles(File directory) Deletes all files in a given directory.static intcleanUpDirectoryFiles(File directory, int maxLimit) Cleans-up a given directory by keeping the number of files within a given limit.static StringcomputeRelativePath(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 booleanDeprecated.static booleanDeprecated.use Java 7 for this seeFiles.copy(InputStream, Path, CopyOption...)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 FilecreateTempFile(String prefix, String suffix) Deprecated.use Java 7 method for this seeFile.createTempFile(String, String, File)static booleandeleteDirectory(File dir) Deletes a given directory, including all its subdirectories and files.static longextractDirectoryFromJar(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 longextractFilesFromJar(JarFile jarFile, File targetDir) Extracts all files from a given JAR file to a given target directory.static longextractFilesFromJar(JarFile jarFile, File targetDir, FileFilter filter) Extracts files from a given JAR file to a given target directory, based on a givenFileFilterobject.static longextractFilesWithExtFromJar(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 StringgetAbsolutePath(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 StringgetFileNameExtension(String fileName) Identifies a given file name extension.static longgetFileSize(String fileLocation) Deprecated.use Java 7 method for this seeFiles.size(Path)static StringgetRelativePath(File rootDir, String absolutePath) Constructs a relative path of a given object, located in a given root directory, based on its absolute path.static StringidentifyUtfSignature(int[] prefix, int length) Makes and attempt to identify possible UTF signature (BOM) in a given sequence of bytes.static booleanisAsciiFile(File textFile) Returnstrue, if a given text file contains only ASCII characters, otherwise returnsfalse.static booleanisAsciiStream(InputStream iStream) Returnstrue, if a given input stream contains only ASCII characters, otherwise returnsfalse.static String[]loadListOfStrings(BufferedReader iStream) Loads a list of non-empty EOL-delimited strings from a given text stream.static String[]loadListOfStrings(File textFile) Deprecated.use Java 7 method for this seeFiles.readAllLines(Path, Charset)static String[]loadListOfStrings(URL textFileURL) Loads a list of non-empty EOL-delimited strings from a given remote text file.static PropertiesloadPropertiesFromJar(String propFilePath, JarFile jarFile) Loads a specified properties file from a given JAR file.static StringloadTextFile(BufferedReader iStream) Loads a text file associated with a given input stream.static StringloadTextFile(File textFile) Deprecated.use main file util for this, seeFileUtils.file2String(File)if using the default charset is OKstatic StringloadTextFile(File textFile, String encoding) Deprecated.use main file util for this, seeFileUtils.file2String(File, String)if using the default Charset is OKstatic StringloadTextFile(URL textFileURL) Loads a given remote text file.static StringloadTextFile(URLConnection urlConnection) Loads a given remote text file.static StringloadTextFileFromJar(String filePath, JarFile jarFile) Loads a specified text file from a given JAR file.static StringlocalPathToFileUrl(String path) Converts a given input file path into a valid file URL string.static booleanDeprecated.use Java 7 for this seeFiles.move(Path, Path, CopyOption...)static intreplaceStringInFile(File textFile, String subStringRegex, String replacement) Replaces all occurrences of a given regular expression with a given string in a given text file.sortFileListByTime(Collection<File> fileList) Sorts a given list of files by the 'last modified' time in the descending order.static FilezipDirectory(File dir2zip) Zips the contents of a given directory.static FilezipDirectory(File dir2zip, File zippedFile) Zips the contents of a given directory to a given output ZIP file.static ZipOutputStreamzipDirectory(File dir2zip, ZipOutputStream zoStream, File referenceDir, File[] excludeFiles) Zips the contents of a given directory to a given ZIP output stream.static FileZips a given file.static FileZips a given file to a given output ZIP file.
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
cleanUpDirectoryContent
Deletes all files and subdirectories in a given directory. In case of unsuccessful deletion, calls thedeleteOnExit()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
Deletes all files in a given directory. In case of unsuccessful deletion, calls thedeleteOnExit()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
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 thedeleteOnExit()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
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
Deprecated.use Java 7 for this seeFiles.copy(Path, Path, CopyOption...)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:
trueif the copy operation completed successfully,falseotherwise.- Throws:
IOException- If any I/O exception occurred.
-
copyFile
Deprecated.use Java 7 for this seeFiles.copy(InputStream, Path, CopyOption...)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:
trueif the copy operation completed successfully,falseotherwise.- Throws:
IOException- If any I/O exception occurred.
-
createDirList
Creates list of subdirectories in a given root directory, including all its subdirectories.- Parameters:
rootDir- The given root directory.- Returns:
CollectionofFileobjects, 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 givenbooleanflag istrue, 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- Iftrue, 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:
CollectionofFileobjects, representing subdirectories in the given root directory.- Throws:
IOException- If any I/O exception occurs.
-
createDirList
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:
CollectionofFileobjects, representing directories in the given archive file.- Throws:
IOException- If any I/O exception occurs.
-
createFileList
Creates list of files in a given directory, including all its subdirectories.- Parameters:
filesDir- The given directory.- Returns:
CollectionofFileobjects 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 givenbooleanflag istrue, 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- Iftrue, 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:
CollectionofFileobjects in the given directory.- Throws:
IOException- If any I/O exception occurs.
-
createFileList
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:
CollectionofFileobjects, representing files in the given archive file.- Throws:
IOException- If any I/O exception occurs.
-
createTempFile
Deprecated.use Java 7 method for this seeFile.createTempFile(String, String, File)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 thejava.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 benull, in which case the suffix ".tmp" will be used.- Returns:
- The
Fileobject denoting the newly created file. - Throws:
IOException- If a temporary directory not found or other I/O exception occurred.
-
deleteDirectory
Deletes a given directory, including all its subdirectories and files. Returnstrueif the deletion was successful, otherwise returnsfalse. In case of unsuccessful deletion, callsdeleteOnExit()method to request that files and subdirs be deleted when the virtual machine terminates.- Parameters:
dir- The given directory to be deleted.- Returns:
trueif the deletion was successful, otherwisefalse.- 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, usenullas thefileExtparameter.- 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
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 givenFileFilterobject.- Parameters:
jarFile- The given JAR file.targetDir- The given target directory.filter- The givenFileFilterobject.- Returns:
- Total number of bytes extracted.
- Throws:
IOException- If any I/O exception occurred.
-
getAbsolutePath
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
Identifies a given file name extension.- Parameters:
fileName- The given file name.- Returns:
- The file name extension
-
getFileSize
Deprecated.use Java 7 method for this seeFiles.size(Path)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
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
Makes and attempt to identify possible UTF signature (BOM) in a given sequence of bytes. Returns the identified UTF signature name ornull, 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
Returnstrue, if a given text file contains only ASCII characters, otherwise returnsfalse.- Parameters:
textFile- The given text file.- Returns:
true, if the given text file contains only ASCII characters,falseotherwise.- Throws:
IOException- If an I/O exception occurred.
-
isAsciiStream
Returnstrue, if a given input stream contains only ASCII characters, otherwise returnsfalse.- Parameters:
iStream- The given input stream.- Returns:
true, if the given input stream contains only ASCII characters,falseotherwise.- Throws:
IOException- If an I/O exception occurred.
-
loadListOfStrings
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
Deprecated.use Java 7 method for this seeFiles.readAllLines(Path, Charset)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
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:
Propertiesobject containing loaded properties, ornull, if the properties file was not found in the given JAR file.- Throws:
IOException- If any I/O exception occurred.
-
loadTextFile
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
Deprecated.use main file util for this, seeFileUtils.file2String(File)if using the default charset is OKLoads 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
Deprecated.use main file util for this, seeFileUtils.file2String(File, String)if using the default Charset is OKLoads 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
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
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
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
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
Deprecated.use Java 7 for this seeFiles.move(Path, Path, CopyOption...)Moves a given source file to a given destination directory.- Parameters:
source- The given source file.destinationDir- The given destination directory.- Returns:
trueif the move operation completed successfully,falseotherwise.- Throws:
IOException- If any I/O exception occurred.
-
replaceStringInFile
public static int replaceStringInFile(File textFile, String subStringRegex, String replacement) throws IOException Replaces all occurrences 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
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
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
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 isnull, 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 ornull.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
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
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.
-
Files.copy(Path, Path, CopyOption...)