Package org.apache.uima.pear.util
Class FileUtil
- java.lang.Object
-
- org.apache.uima.pear.util.FileUtil
-
public class FileUtil extends java.lang.Object
TheFileUtil
class provides utility methods for working with general files.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileUtil.DirFileFilter
TheDirFileFilter
class allows to filter files based on specified directory path and filename extension.static class
FileUtil.ExtFilenameFilter
TheExtFileFilter
allows to filter file names based on the specified filename extension.static class
FileUtil.FileTimeComparator
TheFileTimeComparator
class allows comparing 'last modified' time in 2 givenFile
objects.static class
FileUtil.NameFileFilter
TheNameFileFilter
class allows to filter files based on specified file name.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
ASCII_ENCODING
private static java.lang.String
BACKUP_EXTENSION
private static char
DOT
private static java.lang.String
UNIX_SEPARATOR
private static char
UNIX_SEPARATOR_CHAR
private static java.lang.String
UTF8_ENCODING
private static java.lang.String
WINDOWS_SEPARATOR
private static char
WINDOWS_SEPARATOR_CHAR
private static java.lang.String
XML_EXTENSION
private static java.lang.String
ZIP_EXTENSION
-
Constructor Summary
Constructors Constructor Description FileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static int
cleanUpDirectoryContent(java.io.File directory)
Deletes all files and subdirectories in a given directory.static int
cleanUpDirectoryFiles(java.io.File directory)
Deletes all files in a given directory.static int
cleanUpDirectoryFiles(java.io.File directory, int maxLimit)
Cleans-up a given directory by keeping the number of files within a given limit.static java.lang.String
computeRelativePath(java.io.File referenceDir, java.io.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(java.io.File source, java.io.File destination)
Deprecated.use Java 7 for this seeFiles.copy(Path, Path, CopyOption...)
static boolean
copyFile(java.net.URL sourceUrl, java.io.File destination)
Deprecated.use Java 7 for this seeFiles.copy(InputStream, Path, CopyOption...)
static java.util.Collection<java.io.File>
createDirList(java.io.File rootDir)
Creates list of subdirectories in a given root directory, including all its subdirectories.static java.util.Collection<java.io.File>
createDirList(java.io.File rootDir, boolean includeSubdirs)
Creates list of subdirectories in a given root directory.static java.util.Collection<java.io.File>
createDirList(java.util.jar.JarFile archive)
Creates a list of directories in a given archive (JAR) file.static java.util.Collection<java.io.File>
createFileList(java.io.File filesDir)
Creates list of files in a given directory, including all its subdirectories.static java.util.Collection<java.io.File>
createFileList(java.io.File filesDir, boolean includeSubdirs)
Creates list of files in a given directory.static java.util.Collection<java.io.File>
createFileList(java.util.jar.JarFile archive)
Creates a list of files in a given archive (JAR) file.static java.io.File
createTempFile(java.lang.String prefix, java.lang.String suffix)
Deprecated.use Java 7 method for this seeFile.createTempFile(String, String, File)
static boolean
deleteDirectory(java.io.File dir)
Deletes a given directory, including all its subdirectories and files.static long
extractDirectoryFromJar(java.util.jar.JarFile jarFile, java.lang.String dirPath, java.io.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(java.util.jar.JarFile jarFile, java.io.File targetDir)
Extracts all files from a given JAR file to a given target directory.static long
extractFilesFromJar(java.util.jar.JarFile jarFile, java.io.File targetDir, java.io.FileFilter filter)
Extracts files from a given JAR file to a given target directory, based on a givenFileFilter
object.static long
extractFilesWithExtFromJar(java.util.jar.JarFile jarFile, java.lang.String fileExt, java.io.File targetDir)
Extracts all files that have a given extension from a given JAR file to a given target directory.static java.lang.String
getAbsolutePath(java.io.File rootDir, java.lang.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 java.lang.String
getFileNameExtension(java.lang.String fileName)
Identifies a given file name extension.static long
getFileSize(java.lang.String fileLocation)
Deprecated.use Java 7 method for this seeFiles.size(Path)
static java.lang.String
getRelativePath(java.io.File rootDir, java.lang.String absolutePath)
Constructs a relative path of a given object, located in a given root directory, based on its absolute path.static java.lang.String
identifyUtfSignature(int[] prefix, int length)
Makes and attempt to identify possible UTF signature (BOM) in a given sequence of bytes.static boolean
isAsciiFile(java.io.File textFile)
Returnstrue
, if a given text file contains only ASCII characters, otherwise returnsfalse
.static boolean
isAsciiStream(java.io.InputStream iStream)
Returnstrue
, if a given input stream contains only ASCII characters, otherwise returnsfalse
.static java.lang.String[]
loadListOfStrings(java.io.BufferedReader iStream)
Loads a list of non-empty EOL-delimited strings from a given text stream.static java.lang.String[]
loadListOfStrings(java.io.File textFile)
Deprecated.use Java 7 method for this seeFiles.readAllLines(Path, Charset)
static java.lang.String[]
loadListOfStrings(java.net.URL textFileURL)
Loads a list of non-empty EOL-delimited strings from a given remote text file.static java.util.Properties
loadPropertiesFromJar(java.lang.String propFilePath, java.util.jar.JarFile jarFile)
Loads a specified properties file from a given JAR file.static java.lang.String
loadTextFile(java.io.BufferedReader iStream)
Loads a text file associated with a given input stream.static java.lang.String
loadTextFile(java.io.File textFile)
Deprecated.use main file util for this, seeFileUtils.file2String(File)
if using the default charset is OKstatic java.lang.String
loadTextFile(java.io.File textFile, java.lang.String encoding)
Deprecated.use main file util for this, seeFileUtils.file2String(File, String)
if using the default Charset is OKstatic java.lang.String
loadTextFile(java.net.URL textFileURL)
Loads a given remote text file.static java.lang.String
loadTextFile(java.net.URLConnection urlConnection)
Loads a given remote text file.static java.lang.String
loadTextFileFromJar(java.lang.String filePath, java.util.jar.JarFile jarFile)
Loads a specified text file from a given JAR file.static java.lang.String
localPathToFileUrl(java.lang.String path)
Converts a given input file path into a valid file URL string.static boolean
moveFile(java.io.File source, java.io.File destinationDir)
Deprecated.use Java 7 for this seeFiles.move(Path, Path, CopyOption...)
private static java.lang.String
normalizeToUnix(java.lang.String aPath)
static int
replaceStringInFile(java.io.File textFile, java.lang.String subStringRegex, java.lang.String replacement)
Replaces all occurrences of a given regular expression with a given string in a given text file.static java.util.SortedSet<java.io.File>
sortFileListByTime(java.util.Collection<java.io.File> fileList)
Sorts a given list of files by the 'last modified' time in the descending order.static java.io.File
zipDirectory(java.io.File dir2zip)
Zips the contents of a given directory.static java.io.File
zipDirectory(java.io.File dir2zip, java.io.File zippedFile)
Zips the contents of a given directory to a given output ZIP file.static java.util.zip.ZipOutputStream
zipDirectory(java.io.File dir2zip, java.util.zip.ZipOutputStream zoStream, java.io.File referenceDir, java.io.File[] excludeFiles)
Zips the contents of a given directory to a given ZIP output stream.static java.io.File
zipFile(java.io.File file2zip)
Zips a given file.static java.io.File
zipFile(java.io.File file2zip, java.io.File zippedFile)
Zips a given file to a given output ZIP file.
-
-
-
Field Detail
-
DOT
private static final char DOT
- See Also:
- Constant Field Values
-
UTF8_ENCODING
private static final java.lang.String UTF8_ENCODING
- See Also:
- Constant Field Values
-
ASCII_ENCODING
private static final java.lang.String ASCII_ENCODING
- See Also:
- Constant Field Values
-
XML_EXTENSION
private static final java.lang.String XML_EXTENSION
- See Also:
- Constant Field Values
-
BACKUP_EXTENSION
private static final java.lang.String BACKUP_EXTENSION
- See Also:
- Constant Field Values
-
ZIP_EXTENSION
private static final java.lang.String ZIP_EXTENSION
- See Also:
- Constant Field Values
-
UNIX_SEPARATOR_CHAR
private static final char UNIX_SEPARATOR_CHAR
- See Also:
- Constant Field Values
-
WINDOWS_SEPARATOR_CHAR
private static final char WINDOWS_SEPARATOR_CHAR
- See Also:
- Constant Field Values
-
UNIX_SEPARATOR
private static final java.lang.String UNIX_SEPARATOR
-
WINDOWS_SEPARATOR
private static final java.lang.String WINDOWS_SEPARATOR
-
-
Method Detail
-
cleanUpDirectoryContent
public static int cleanUpDirectoryContent(java.io.File directory) throws java.io.IOException
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:
java.io.IOException
- If an I/O exception occurred.
-
cleanUpDirectoryFiles
public static int cleanUpDirectoryFiles(java.io.File directory) throws java.io.IOException
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:
java.io.IOException
- If an I/O exception occurred.
-
cleanUpDirectoryFiles
public static int cleanUpDirectoryFiles(java.io.File directory, int maxLimit) throws java.io.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 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:
java.io.IOException
- If an I/O exception occurred.
-
normalizeToUnix
private static java.lang.String normalizeToUnix(java.lang.String aPath)
-
computeRelativePath
public static java.lang.String computeRelativePath(java.io.File referenceDir, java.io.File file) throws java.io.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:
java.io.IOException
- If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries.
-
copyFile
@Deprecated public static boolean copyFile(java.io.File source, java.io.File destination) throws java.io.IOException
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:
true
if the copy operation completed successfully,false
otherwise.- Throws:
java.io.IOException
- If any I/O exception occurred.
-
copyFile
@Deprecated public static boolean copyFile(java.net.URL sourceUrl, java.io.File destination) throws java.io.IOException
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:
true
if the copy operation completed successfully,false
otherwise.- Throws:
java.io.IOException
- If any I/O exception occurred.
-
createDirList
public static java.util.Collection<java.io.File> createDirList(java.io.File rootDir) throws java.io.IOException
Creates list of subdirectories in a given root directory, including all its subdirectories.- Parameters:
rootDir
- The given root directory.- Returns:
Collection
ofFile
objects, representing subdirectories in the given root directory and all its subdirectories.- Throws:
java.io.IOException
- If any I/O exception occurs.
-
createDirList
public static java.util.Collection<java.io.File> createDirList(java.io.File rootDir, boolean includeSubdirs) throws java.io.IOException
Creates list of subdirectories in a given root directory. If a givenboolean
flag 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:
Collection
ofFile
objects, representing subdirectories in the given root directory.- Throws:
java.io.IOException
- If any I/O exception occurs.
-
createDirList
public static java.util.Collection<java.io.File> createDirList(java.util.jar.JarFile archive) throws java.io.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
ofFile
objects, representing directories in the given archive file.- Throws:
java.io.IOException
- If any I/O exception occurs.
-
createFileList
public static java.util.Collection<java.io.File> createFileList(java.io.File filesDir) throws java.io.IOException
Creates list of files in a given directory, including all its subdirectories.- Parameters:
filesDir
- The given directory.- Returns:
Collection
ofFile
objects in the given directory, including all its subdirectories.- Throws:
java.io.IOException
- If any I/O exception occurs.
-
createFileList
public static java.util.Collection<java.io.File> createFileList(java.io.File filesDir, boolean includeSubdirs) throws java.io.IOException
Creates list of files in a given directory. If a givenboolean
flag 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:
Collection
ofFile
objects in the given directory.- Throws:
java.io.IOException
- If any I/O exception occurs.
-
createFileList
public static java.util.Collection<java.io.File> createFileList(java.util.jar.JarFile archive) throws java.io.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
ofFile
objects, representing files in the given archive file.- Throws:
java.io.IOException
- If any I/O exception occurs.
-
createTempFile
@Deprecated public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException
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
File
object denoting the newly created file. - Throws:
java.io.IOException
- If a temporary directory not found or other I/O exception occurred.
-
deleteDirectory
public static boolean deleteDirectory(java.io.File dir) throws java.io.IOException
Deletes a given directory, including all its subdirectories and files. Returnstrue
if 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:
true
if the deletion was successful, otherwisefalse
.- Throws:
java.io.IOException
- If any I/O exception occurs.
-
extractDirectoryFromJar
public static long extractDirectoryFromJar(java.util.jar.JarFile jarFile, java.lang.String dirPath, java.io.File targetDir) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
extractFilesWithExtFromJar
public static long extractFilesWithExtFromJar(java.util.jar.JarFile jarFile, java.lang.String fileExt, java.io.File targetDir) throws java.io.IOException
Extracts all files that have a given extension from a given JAR file to a given target directory. To extract files without extension, usenull
as thefileExt
parameter.- Parameters:
jarFile
- The given JAR file.fileExt
- The given file extension.targetDir
- The given target directory.- Returns:
- Total number of bytes extracted.
- Throws:
java.io.IOException
- If any I/O exception occurred.
-
extractFilesFromJar
public static long extractFilesFromJar(java.util.jar.JarFile jarFile, java.io.File targetDir) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
extractFilesFromJar
public static long extractFilesFromJar(java.util.jar.JarFile jarFile, java.io.File targetDir, java.io.FileFilter filter) throws java.io.IOException
Extracts files from a given JAR file to a given target directory, based on a givenFileFilter
object.- Parameters:
jarFile
- The given JAR file.targetDir
- The given target directory.filter
- The givenFileFilter
object.- Returns:
- Total number of bytes extracted.
- Throws:
java.io.IOException
- If any I/O exception occurred.
-
getAbsolutePath
public static java.lang.String getAbsolutePath(java.io.File rootDir, java.lang.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 java.lang.String getFileNameExtension(java.lang.String fileName)
Identifies a given file name extension.- Parameters:
fileName
- The given file name.- Returns:
- The file name extension
-
getFileSize
@Deprecated public static long getFileSize(java.lang.String fileLocation)
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
public static java.lang.String getRelativePath(java.io.File rootDir, java.lang.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 java.lang.String identifyUtfSignature(int[] prefix, int length)
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
public static boolean isAsciiFile(java.io.File textFile) throws java.io.IOException
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,false
otherwise.- Throws:
java.io.IOException
- If an I/O exception occurred.
-
isAsciiStream
public static boolean isAsciiStream(java.io.InputStream iStream) throws java.io.IOException
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,false
otherwise.- Throws:
java.io.IOException
- If an I/O exception occurred.
-
loadListOfStrings
public static java.lang.String[] loadListOfStrings(java.io.BufferedReader iStream) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
loadListOfStrings
@Deprecated public static java.lang.String[] loadListOfStrings(java.io.File textFile) throws java.io.IOException
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:
java.io.IOException
- If any I/O exception occurred.
-
loadListOfStrings
public static java.lang.String[] loadListOfStrings(java.net.URL textFileURL) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
loadPropertiesFromJar
public static java.util.Properties loadPropertiesFromJar(java.lang.String propFilePath, java.util.jar.JarFile jarFile) throws java.io.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, ornull
, if the properties file was not found in the given JAR file.- Throws:
java.io.IOException
- If any I/O exception occurred.
-
loadTextFile
public static java.lang.String loadTextFile(java.io.BufferedReader iStream) throws java.io.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:
java.io.IOException
- If any I/O exception occurs.
-
loadTextFile
@Deprecated public static java.lang.String loadTextFile(java.io.File textFile) throws java.io.IOException
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:
java.io.IOException
- If any I/O exception occurs.
-
loadTextFile
@Deprecated public static java.lang.String loadTextFile(java.io.File textFile, java.lang.String encoding) throws java.io.IOException
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:
java.io.IOException
- If any I/O exception occurs.
-
loadTextFile
public static java.lang.String loadTextFile(java.net.URL textFileURL) throws java.io.IOException
Loads a given remote text file.- Parameters:
textFileURL
- The given text file URL.- Returns:
- The content of the text file.
- Throws:
java.io.IOException
- If any I/O exception occurs.
-
loadTextFile
public static java.lang.String loadTextFile(java.net.URLConnection urlConnection) throws java.io.IOException
Loads a given remote text file.- Parameters:
urlConnection
- The given URL connection.- Returns:
- The content of the text file.
- Throws:
java.io.IOException
- If any I/O exception occurs.
-
loadTextFileFromJar
public static java.lang.String loadTextFileFromJar(java.lang.String filePath, java.util.jar.JarFile jarFile) throws java.io.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:
java.io.IOException
- If any I/O exception occurs.
-
localPathToFileUrl
public static java.lang.String localPathToFileUrl(java.lang.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
@Deprecated public static boolean moveFile(java.io.File source, java.io.File destinationDir) throws java.io.IOException
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:
true
if the move operation completed successfully,false
otherwise.- Throws:
java.io.IOException
- If any I/O exception occurred.
-
replaceStringInFile
public static int replaceStringInFile(java.io.File textFile, java.lang.String subStringRegex, java.lang.String replacement) throws java.io.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:
java.io.IOException
- If any I/O exception occurs.
-
sortFileListByTime
public static java.util.SortedSet<java.io.File> sortFileListByTime(java.util.Collection<java.io.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 java.io.File zipDirectory(java.io.File dir2zip) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
zipDirectory
public static java.io.File zipDirectory(java.io.File dir2zip, java.io.File zippedFile) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
zipDirectory
public static java.util.zip.ZipOutputStream zipDirectory(java.io.File dir2zip, java.util.zip.ZipOutputStream zoStream, java.io.File referenceDir, java.io.File[] excludeFiles) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
zipFile
public static java.io.File zipFile(java.io.File file2zip) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
zipFile
public static java.io.File zipFile(java.io.File file2zip, java.io.File zippedFile) throws java.io.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:
java.io.IOException
- If any I/O exception occurred.
-
-