public class FileUtil
extends java.lang.Object
FileUtil
class provides utility methods for working with general files.Modifier and Type | Class and Description |
---|---|
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 and Description |
---|
FileUtil() |
Modifier and Type | Method and 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)
Copies the content of a given source file to a given destination file.
|
static boolean |
copyFile(java.net.URL sourceUrl,
java.io.File destination)
Copies the content of a given remote source file to a given destination file.
|
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)
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(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 given
FileFilter 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)
Returns file size for a given file.
|
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)
Returns
true , if a given text file contains only ASCII characters, otherwise
returns false . |
static boolean |
isAsciiStream(java.io.InputStream iStream)
Returns
true , if a given input stream contains only ASCII characters, otherwise
returns false . |
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)
Loads a list of non-empty EOL-delimited strings from a given text file using the default file
encoding.
|
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)
Loads a given local text file using the default file encoding.
|
static java.lang.String |
loadTextFile(java.io.File textFile,
java.lang.String encoding)
Loads a given local text file using a specified file encoding.
|
static 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)
Moves a given source file to a given destination directory.
|
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.
|
public static int cleanUpDirectoryContent(java.io.File directory) throws java.io.IOException
deleteOnExit()
method to request that files and subdirs are deleted
when the JVM terminates.directory
- The given directory to be cleaned-up.java.io.IOException
- If an I/O exception occurred.public static int cleanUpDirectoryFiles(java.io.File directory) throws java.io.IOException
deleteOnExit()
method to request that files are deleted when the JVM terminates.directory
- The given directory to be cleaned-up.java.io.IOException
- If an I/O exception occurred.public static int cleanUpDirectoryFiles(java.io.File directory, int maxLimit) throws java.io.IOException
deleteOnExit()
method to request that files are deleted when the JVM terminates.directory
- The given directory.maxLimit
- The given maximum limit of the number of files in the given directory.java.io.IOException
- If an I/O exception occurred.public static java.lang.String computeRelativePath(java.io.File referenceDir, java.io.File file) throws java.io.IOException
referenceDir
- The given reference directory.file
- The given file.null
, if the relative path does not exist.java.io.IOException
- If an I/O error occurs, which is possible because the construction of the canonical
pathname may require filesystem queries.public static boolean copyFile(java.io.File source, java.io.File destination) throws java.io.IOException
source
- The given source file.destination
- The given destination file.true
if the copy operation completed successfully, false
otherwise.java.io.IOException
- If any I/O exception occurred.public static boolean copyFile(java.net.URL sourceUrl, java.io.File destination) throws java.io.IOException
sourceUrl
- The given source file URL.destination
- The given destination file.true
if the copy operation completed successfully, false
otherwise.java.io.IOException
- If any I/O exception occurred.public static java.util.Collection<java.io.File> createDirList(java.io.File rootDir) throws java.io.IOException
rootDir
- The given root directory.Collection
of File
objects, representing subdirectories
in the given root directory and all its subdirectories.java.io.IOException
- If any I/O exception occurs.public static java.util.Collection<java.io.File> createDirList(java.io.File rootDir, boolean includeSubdirs) throws java.io.IOException
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.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.Collection
of File
objects, representing subdirectories
in the given root directory.java.io.IOException
- If any I/O exception occurs.public static java.util.Collection<java.io.File> createDirList(java.util.jar.JarFile archive) throws java.io.IOException
archive
- The input archive (JAR) file.Collection
of File
objects, representing directories in
the given archive file.java.io.IOException
- If any I/O exception occurs.public static java.util.Collection<java.io.File> createFileList(java.io.File filesDir) throws java.io.IOException
filesDir
- The given directory.Collection
of File
objects in the given directory,
including all its subdirectories.java.io.IOException
- If any I/O exception occurs.public static java.util.Collection<java.io.File> createFileList(java.io.File filesDir, boolean includeSubdirs) throws java.io.IOException
boolean
flag is
true
, all the sub-directories of the given directory are also scanned,
otherwise only files in the given directory are included.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.Collection
of File
objects in the given directory.java.io.IOException
- If any I/O exception occurs.public static java.util.Collection<java.io.File> createFileList(java.util.jar.JarFile archive) throws java.io.IOException
archive
- The input archive (JAR) file.Collection
of File
objects, representing files in the
given archive file.java.io.IOException
- If any I/O exception occurs.public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException
java.io.File.createTempFile()
method.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.File
object denoting the newly created file.java.io.IOException
- If a temporary directory not found or other I/O exception occurred.public static boolean deleteDirectory(java.io.File dir) throws java.io.IOException
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.dir
- The given directory to be deleted.true
if the deletion was successful, otherwise false
.java.io.IOException
- If any I/O exception occurs.public static long extractDirectoryFromJar(java.util.jar.JarFile jarFile, java.lang.String dirPath, java.io.File targetDir) throws java.io.IOException
jarFile
- The given JAR file.dirPath
- The given JAR directory.targetDir
- The given target directory.java.io.IOException
- If any I/O exception occurred.public static long extractFilesWithExtFromJar(java.util.jar.JarFile jarFile, java.lang.String fileExt, java.io.File targetDir) throws java.io.IOException
null
as the
fileExt
parameter.jarFile
- The given JAR file.fileExt
- The given file extension.targetDir
- The given target directory.java.io.IOException
- If any I/O exception occurred.public static long extractFilesFromJar(java.util.jar.JarFile jarFile, java.io.File targetDir) throws java.io.IOException
jarFile
- The given JAR file.targetDir
- The given target directory.java.io.IOException
- If any I/O exception occurred.public static long extractFilesFromJar(java.util.jar.JarFile jarFile, java.io.File targetDir, java.io.FileFilter filter) throws java.io.IOException
FileFilter
object.jarFile
- The given JAR file.targetDir
- The given target directory.filter
- The given FileFilter
object.java.io.IOException
- If any I/O exception occurred.public static java.lang.String getAbsolutePath(java.io.File rootDir, java.lang.String relativePath)
rootDir
- The given root directory.relativePath
- The given relative path of the object.public static java.lang.String getFileNameExtension(java.lang.String fileName)
fileName
- The given file name.public static long getFileSize(java.lang.String fileLocation)
fileLocation
- The given file location - local file path or URL.public static java.lang.String getRelativePath(java.io.File rootDir, java.lang.String absolutePath)
rootDir
- The given root directory.absolutePath
- The given absolute path of the object.public static java.lang.String identifyUtfSignature(int[] prefix, int length)
null
, if the signature could not
be identified. For more on UTF and its signatures see FAQ - UTF and BOM.prefix
- The given sequence of bytes to analyze.length
- The length of the given sequence of bytes.null
, if the signature could not be
identified.public static boolean isAsciiFile(java.io.File textFile) throws java.io.IOException
true
, if a given text file contains only ASCII characters, otherwise
returns false
.textFile
- The given text file.true
, if the given text file contains only ASCII characters,
false
otherwise.java.io.IOException
- If an I/O exception occurred.public static boolean isAsciiStream(java.io.InputStream iStream) throws java.io.IOException
true
, if a given input stream contains only ASCII characters, otherwise
returns false
.iStream
- The given input stream.true
, if the given input stream contains only ASCII characters,
false
otherwise.java.io.IOException
- If an I/O exception occurred.public static java.lang.String[] loadListOfStrings(java.io.BufferedReader iStream) throws java.io.IOException
iStream
- The given input text stream.java.io.IOException
- If any I/O exception occurred.public static java.lang.String[] loadListOfStrings(java.io.File textFile) throws java.io.IOException
textFile
- The given text file.java.io.IOException
- If any I/O exception occurred.public static java.lang.String[] loadListOfStrings(java.net.URL textFileURL) throws java.io.IOException
textFileURL
- The URL of the given input text file.java.io.IOException
- If any I/O exception occurred.public static java.util.Properties loadPropertiesFromJar(java.lang.String propFilePath, java.util.jar.JarFile jarFile) throws java.io.IOException
propFilePath
- The given properties file path in the JAR file.jarFile
- The given JAR file.Properties
object containing loaded properties, or null
,
if the properties file was not found in the given JAR file.java.io.IOException
- If any I/O exception occurred.public static java.lang.String loadTextFile(java.io.BufferedReader iStream) throws java.io.IOException
iStream
- The given text input stream.java.io.IOException
- If any I/O exception occurs.public static java.lang.String loadTextFile(java.io.File textFile) throws java.io.IOException
textFile
- The given text file.java.io.IOException
- If any I/O exception occurs.public static java.lang.String loadTextFile(java.io.File textFile, java.lang.String encoding) throws java.io.IOException
textFile
- The given text file.encoding
- The given text file encoding name.java.io.IOException
- If any I/O exception occurs.public static java.lang.String loadTextFile(java.net.URL textFileURL) throws java.io.IOException
textFileURL
- The given text file URL.java.io.IOException
- If any I/O exception occurs.public static java.lang.String loadTextFile(java.net.URLConnection urlConnection) throws java.io.IOException
urlConnection
- The given URL connection.java.io.IOException
- If any I/O exception occurs.public static java.lang.String loadTextFileFromJar(java.lang.String filePath, java.util.jar.JarFile jarFile) throws java.io.IOException
filePath
- The specified text file path inside the JAR file.jarFile
- The given JAR file.null
, if the text file was
not found in the given JAR file.java.io.IOException
- If any I/O exception occurs.public static java.lang.String localPathToFileUrl(java.lang.String path)
path
- The given file path to be converted.public static boolean moveFile(java.io.File source, java.io.File destinationDir) throws java.io.IOException
source
- The given source file.destinationDir
- The given destination directory.true
if the move operation completed successfully, false
otherwise.java.io.IOException
- If any I/O exception occurred.public static int replaceStringInFile(java.io.File textFile, java.lang.String subStringRegex, java.lang.String replacement) throws java.io.IOException
textFile
- The given text file.subStringRegex
- The given regular expression string to be replaced.replacement
- The given replacement string.java.io.IOException
- If any I/O exception occurs.public static java.util.SortedSet<java.io.File> sortFileListByTime(java.util.Collection<java.io.File> fileList)
fileList
- The given list of files.public static java.io.File zipDirectory(java.io.File dir2zip) throws java.io.IOException
dir2zip
- The given directory to be zipped.java.io.IOException
- If any I/O exception occurred.public static java.io.File zipDirectory(java.io.File dir2zip, java.io.File zippedFile) throws java.io.IOException
dir2zip
- The given directory to be zipped.zippedFile
- The given output ZIP file.java.io.IOException
- If any I/O exception occurred.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
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.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.java.io.IOException
- If any I/O exception occurred.public static java.io.File zipFile(java.io.File file2zip) throws java.io.IOException
file2zip
- The file to be zipped.java.io.IOException
- If any I/O exception occurred.public static java.io.File zipFile(java.io.File file2zip, java.io.File zippedFile) throws java.io.IOException
file2zip
- The file to be zipped.zippedFile
- The given output ZIP file.java.io.IOException
- If any I/O exception occurred.Copyright © 2006–2014 The Apache Software Foundation. All rights reserved.