public class FileUtil extends 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(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 signature (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 occurrences 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.
|
public static int cleanUpDirectoryContent(File directory) throws IOException
deleteOnExit() method to request that files and subdirs are deleted
when the JVM terminates.directory - The given directory to be cleaned-up.IOException - If an I/O exception occurred.public static int cleanUpDirectoryFiles(File directory) throws IOException
deleteOnExit() method to request that files are deleted when the JVM terminates.directory - The given directory to be cleaned-up.IOException - If an I/O exception occurred.public static int cleanUpDirectoryFiles(File directory, int maxLimit) throws 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.IOException - If an I/O exception occurred.public static String computeRelativePath(File referenceDir, File file) throws IOException
referenceDir - The given reference directory.file - The given file.null, if the relative path does not exist.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(File source, File destination) throws IOException
source - The given source file.destination - The given destination file.true if the copy operation completed successfully, false
otherwise.IOException - If any I/O exception occurred.public static boolean copyFile(URL sourceUrl, File destination) throws IOException
sourceUrl - The given source file URL.destination - The given destination file.true if the copy operation completed successfully, false
otherwise.IOException - If any I/O exception occurred.public static Collection<File> createDirList(File rootDir) throws IOException
rootDir - The given root directory.Collection of File objects, representing subdirectories
in the given root directory and all its subdirectories.IOException - If any I/O exception occurs.public static Collection<File> createDirList(File rootDir, boolean includeSubdirs) throws 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.IOException - If any I/O exception occurs.public static Collection<File> createDirList(JarFile archive) throws IOException
archive - The input archive (JAR) file.Collection of File objects, representing directories in
the given archive file.IOException - If any I/O exception occurs.public static Collection<File> createFileList(File filesDir) throws IOException
filesDir - The given directory.Collection of File objects in the given directory,
including all its subdirectories.IOException - If any I/O exception occurs.public static Collection<File> createFileList(File filesDir, boolean includeSubdirs) throws 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.IOException - If any I/O exception occurs.public static Collection<File> createFileList(JarFile archive) throws IOException
archive - The input archive (JAR) file.Collection of File objects, representing files in the
given archive file.IOException - If any I/O exception occurs.public static File createTempFile(String prefix, String suffix) throws 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.IOException - If a temporary directory not found or other I/O exception occurred.public static boolean deleteDirectory(File dir) throws 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.IOException - If any I/O exception occurs.public static long extractDirectoryFromJar(JarFile jarFile, String dirPath, File targetDir) throws IOException
jarFile - The given JAR file.dirPath - The given JAR directory.targetDir - The given target directory.IOException - If any I/O exception occurred.public static long extractFilesWithExtFromJar(JarFile jarFile, String fileExt, File targetDir) throws IOException
null as the
fileExt parameter.jarFile - The given JAR file.fileExt - The given file extension.targetDir - The given target directory.IOException - If any I/O exception occurred.public static long extractFilesFromJar(JarFile jarFile, File targetDir) throws IOException
jarFile - The given JAR file.targetDir - The given target directory.IOException - If any I/O exception occurred.public static long extractFilesFromJar(JarFile jarFile, File targetDir, FileFilter filter) throws IOException
FileFilter object.jarFile - The given JAR file.targetDir - The given target directory.filter - The given FileFilter object.IOException - If any I/O exception occurred.public static String getAbsolutePath(File rootDir, String relativePath)
rootDir - The given root directory.relativePath - The given relative path of the object.public static String getFileNameExtension(String fileName)
fileName - The given file name.public static long getFileSize(String fileLocation)
fileLocation - The given file location - local file path or URL.public static String getRelativePath(File rootDir, String absolutePath)
rootDir - The given root directory.absolutePath - The given absolute path of the object.public static 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(File textFile) throws 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.IOException - If an I/O exception occurred.public static boolean isAsciiStream(InputStream iStream) throws 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.IOException - If an I/O exception occurred.public static String[] loadListOfStrings(BufferedReader iStream) throws IOException
iStream - The given input text stream.IOException - If any I/O exception occurred.public static String[] loadListOfStrings(File textFile) throws IOException
textFile - The given text file.IOException - If any I/O exception occurred.public static String[] loadListOfStrings(URL textFileURL) throws IOException
textFileURL - The URL of the given input text file.IOException - If any I/O exception occurred.public static Properties loadPropertiesFromJar(String propFilePath, JarFile jarFile) throws 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.IOException - If any I/O exception occurred.public static String loadTextFile(BufferedReader iStream) throws IOException
iStream - The given text input stream.IOException - If any I/O exception occurs.public static String loadTextFile(File textFile) throws IOException
textFile - The given text file.IOException - If any I/O exception occurs.public static String loadTextFile(File textFile, String encoding) throws IOException
textFile - The given text file.encoding - The given text file encoding name.IOException - If any I/O exception occurs.public static String loadTextFile(URL textFileURL) throws IOException
textFileURL - The given text file URL.IOException - If any I/O exception occurs.public static String loadTextFile(URLConnection urlConnection) throws IOException
urlConnection - The given URL connection.IOException - If any I/O exception occurs.public static String loadTextFileFromJar(String filePath, JarFile jarFile) throws 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.IOException - If any I/O exception occurs.public static String localPathToFileUrl(String path)
path - The given file path to be converted.public static boolean moveFile(File source, File destinationDir) throws IOException
source - The given source file.destinationDir - The given destination directory.true if the move operation completed successfully, false
otherwise.IOException - If any I/O exception occurred.public static int replaceStringInFile(File textFile, String subStringRegex, String replacement) throws IOException
textFile - The given text file.subStringRegex - The given regular expression string to be replaced.replacement - The given replacement string.IOException - If any I/O exception occurs.public static SortedSet<File> sortFileListByTime(Collection<File> fileList)
fileList - The given list of files.public static File zipDirectory(File dir2zip) throws IOException
dir2zip - The given directory to be zipped.IOException - If any I/O exception occurred.public static File zipDirectory(File dir2zip, File zippedFile) throws IOException
dir2zip - The given directory to be zipped.zippedFile - The given output ZIP file.IOException - If any I/O exception occurred.public static ZipOutputStream zipDirectory(File dir2zip, ZipOutputStream zoStream, File referenceDir, File[] excludeFiles) throws 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.IOException - If any I/O exception occurred.public static File zipFile(File file2zip) throws IOException
file2zip - The file to be zipped.IOException - If any I/O exception occurred.public static File zipFile(File file2zip, File zippedFile) throws IOException
file2zip - The file to be zipped.zippedFile - The given output ZIP file.IOException - If any I/O exception occurred.Copyright © 2006–2017 The Apache Software Foundation. All rights reserved.