byteCountToDisplaySize
public static String byteCountToDisplaySize(long size)
Returns a human-readable version of the file size, where the input
represents a specific number of bytes.
size
- the number of bytes
- a human-readable display value (includes units)
checksum
public static Checksum checksum(File file,
Checksum checksum)
throws IOException
Computes the checksum of a file using the specified checksum object.
Multiple files may be checked using one
Checksum
instance
if desired simply by reusing the same checksum object.
For example:
long csum = FileUtils.checksum(file, new CRC32()).getValue();
file
- the file to checksum, must not be null
checksum
- the checksum object to be used, must not be null
- the checksum specified, updated with the content of the file
checksumCRC32
public static long checksumCRC32(File file)
throws IOException
Computes the checksum of a file using the CRC32 checksum routine.
The value of the checksum is returned.
file
- the file to checksum, must not be null
cleanDirectory
public static void cleanDirectory(File directory)
throws IOException
Clean a directory without deleting it.
directory
- directory to clean
cleanDirectoryOnExit
private static void cleanDirectoryOnExit(File directory)
throws IOException
Clean a directory without deleting it.
directory
- directory to clean, must not be null
contentEquals
public static boolean contentEquals(File file1,
File file2)
throws IOException
Compare the contents of two files to determine if they are equal or not.
This method checks to see if the two files are different lengths
or if they point to the same file, before resorting to byte-by-byte
comparison of the contents.
Code origin: Avalon
file1
- the first filefile2
- the second file
- true if the content of the files are equal or they both don't
exist, false otherwise
convertFileCollectionToFileArray
public static File[] convertFileCollectionToFileArray(Collection files)
Converts a Collection containing java.io.File instanced into array
representation. This is to account for the difference between
File.listFiles() and FileUtils.listFiles().
files
- a Collection containing java.io.File instances
copyDirectory
public static void copyDirectory(File srcDir,
File destDir)
throws IOException
Copies a whole directory to a new location preserving the file dates.
This method copies the specified directory and all its child
directories and files to the specified destination.
The destination is the new location and name of the directory.
The destination directory is created if it does not exist.
If the destination directory did exist, then this method merges
the source with the destination, with the source taking precedence.
srcDir
- an existing directory to copy, must not be null
destDir
- the new directory, must not be null
copyDirectory
public static void copyDirectory(File srcDir,
File destDir,
boolean preserveFileDate)
throws IOException
Copies a whole directory to a new location.
This method copies the contents of the specified source directory
to within the specified destination directory.
The destination directory is created if it does not exist.
If the destination directory did exist, then this method merges
the source with the destination, with the source taking precedence.
srcDir
- an existing directory to copy, must not be null
destDir
- the new directory, must not be null
preserveFileDate
- true if the file date of the copy
should be the same as the original
copyDirectoryToDirectory
public static void copyDirectoryToDirectory(File srcDir,
File destDir)
throws IOException
Copies a directory to within another directory preserving the file dates.
This method copies the source directory and all its contents to a
directory of the same name in the specified destination directory.
The destination directory is created if it does not exist.
If the destination directory did exist, then this method merges
the source with the destination, with the source taking precedence.
srcDir
- an existing directory to copy, must not be null
destDir
- the directory to place the copy in, must not be null
copyFile
public static void copyFile(File srcFile,
File destFile)
throws IOException
Copies a file to a new location preserving the file date.
This method copies the contents of the specified source file to the
specified destination file. The directory holding the destination file is
created if it does not exist. If the destination file exists, then this
method will overwrite it.
srcFile
- an existing file to copy, must not be null
destFile
- the new file, must not be null
copyFile
public static void copyFile(File srcFile,
File destFile,
boolean preserveFileDate)
throws IOException
Copies a file to a new location.
This method copies the contents of the specified source file
to the specified destination file.
The directory holding the destination file is created if it does not exist.
If the destination file exists, then this method will overwrite it.
srcFile
- an existing file to copy, must not be null
destFile
- the new file, must not be null
preserveFileDate
- true if the file date of the copy
should be the same as the original
copyFileToDirectory
public static void copyFileToDirectory(File srcFile,
File destDir)
throws IOException
Copies a file to a directory preserving the file date.
This method copies the contents of the specified source file
to a file of the same name in the specified destination directory.
The destination directory is created if it does not exist.
If the destination file exists, then this method will overwrite it.
srcFile
- an existing file to copy, must not be null
destDir
- the directory to place the copy in, must not be null
copyFileToDirectory
public static void copyFileToDirectory(File srcFile,
File destDir,
boolean preserveFileDate)
throws IOException
Copies a file to a directory optionally preserving the file date.
This method copies the contents of the specified source file
to a file of the same name in the specified destination directory.
The destination directory is created if it does not exist.
If the destination file exists, then this method will overwrite it.
srcFile
- an existing file to copy, must not be null
destDir
- the directory to place the copy in, must not be null
preserveFileDate
- true if the file date of the copy
should be the same as the original
copyURLToFile
public static void copyURLToFile(URL source,
File destination)
throws IOException
Copies bytes from the URL source
to a file
destination
. The directories up to destination
will be created if they don't already exist. destination
will be overwritten if it already exists.
source
- the URL
to copy bytes from, must not be null
destination
- the non-directory File
to write bytes to
(possibly overwriting), must not be null
deleteDirectory
public static void deleteDirectory(File directory)
throws IOException
Recursively delete a directory.
directory
- directory to delete
deleteDirectoryOnExit
private static void deleteDirectoryOnExit(File directory)
throws IOException
Recursively schedule directory for deletion on JVM exit.
directory
- directory to delete, must not be null
doCopyDirectory
private static void doCopyDirectory(File srcDir,
File destDir,
boolean preserveFileDate)
throws IOException
Internal copy directory method.
srcDir
- the validated source directory, must not be null
destDir
- the validated destination directory, must not be null
preserveFileDate
- whether to preserve the file date
doCopyFile
private static void doCopyFile(File srcFile,
File destFile,
boolean preserveFileDate)
throws IOException
Internal copy file method.
srcFile
- the validated source file, must not be null
destFile
- the validated destination file, must not be null
preserveFileDate
- whether to preserve the file date
forceDelete
public static void forceDelete(File file)
throws IOException
Delete a file. If file is a directory, delete it and all sub-directories.
The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- You get exceptions when a file or directory cannot be deleted.
(java.io.File methods returns a boolean)
file
- file or directory to delete, must not be null
forceDeleteOnExit
public static void forceDeleteOnExit(File file)
throws IOException
Schedule a file to be deleted when JVM exits.
If file is directory delete it and all sub-directories.
file
- file or directory to delete, must not be null
forceMkdir
public static void forceMkdir(File directory)
throws IOException
Make a directory, including any necessary but nonexistent parent
directories. If there already exists a file with specified name or
the directory cannot be created then an exception is thrown.
directory
- directory to create, must not be null
innerListFiles
private static void innerListFiles(Collection files,
File directory,
IOFileFilter filter)
Finds files within a given directory (and optionally its
subdirectories). All files found are filtered by an IOFileFilter.
files
- the collection of files found.directory
- the directory to search in.filter
- the filter to apply to files and directories.
isFileNewer
public static boolean isFileNewer(File file,
Date date)
Tests if the specified File
is newer than the specified
Date
.
file
- the File
of which the modification date
must be compared, must not be null
date
- the date reference, must not be null
- true if the
File
exists and has been modified
after the given Date
.
isFileNewer
public static boolean isFileNewer(File file,
File reference)
Tests if the specified File
is newer than the reference
File
.
file
- the File
of which the modification date must
be compared, must not be null
reference
- the File
of which the modification date
is used, must not be null
- true if the
File
exists and has been modified more
recently than the reference File
isFileNewer
public static boolean isFileNewer(File file,
long timeMillis)
Tests if the specified File
is newer than the specified
time reference.
file
- the File
of which the modification date must
be compared, must not be null
timeMillis
- the time reference measured in milliseconds since the
epoch (00:00:00 GMT, January 1, 1970)
- true if the
File
exists and has been modified after
the given time reference.
isFileOlder
public static boolean isFileOlder(File file,
Date date)
Tests if the specified File
is older than the specified
Date
.
file
- the File
of which the modification date
must be compared, must not be null
date
- the date reference, must not be null
- true if the
File
exists and has been modified
before the given Date
.
isFileOlder
public static boolean isFileOlder(File file,
File reference)
Tests if the specified File
is older than the reference
File
.
file
- the File
of which the modification date must
be compared, must not be null
reference
- the File
of which the modification date
is used, must not be null
- true if the
File
exists and has been modified before
the reference File
isFileOlder
public static boolean isFileOlder(File file,
long timeMillis)
Tests if the specified File
is older than the specified
time reference.
file
- the File
of which the modification date must
be compared, must not be null
timeMillis
- the time reference measured in milliseconds since the
epoch (00:00:00 GMT, January 1, 1970)
- true if the
File
exists and has been modified before
the given time reference.
iterateFiles
public static Iterator iterateFiles(File directory,
String[] extensions,
boolean recursive)
Allows iteration over the files in a given directory (and optionally
its subdirectories) which match an array of extensions. This method
is based on
listFiles(File,String[],boolean)
.
directory
- the directory to search inextensions
- an array of extensions, ex. {"java","xml"}. If this
parameter is null
, all files are returned.recursive
- if true all subdirectories are searched as well
- an iterator of java.io.File with the matching files
iterateFiles
public static Iterator iterateFiles(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
directory
- the directory to search infileFilter
- filter to apply when finding files.dirFilter
- optional filter to apply when finding subdirectories.
If this parameter is null
, subdirectories will not be included in the
search. Use TrueFileFilter.INSTANCE to match all directories.
- an iterator of java.io.File for the matching files
lineIterator
public static LineIterator lineIterator(File file)
throws IOException
Return an Iterator for the lines in a File
using the default encoding for the VM.
file
- the file to open for input, must not be null
- an Iterator of the lines in the file, never
null
lineIterator
public static LineIterator lineIterator(File file,
String encoding)
throws IOException
Return an Iterator for the lines in a
File
.
This method opens an
InputStream
for the file.
When you have finished with the iterator you should close the stream
to free internal resources. This can be done by calling the
LineIterator.close()
or
LineIterator.closeQuietly(LineIterator)
method.
The recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
/// do something with line
}
} finally {
LineIterator.closeQuietly(iterator);
}
If an exception occurs during the creation of the iterator, the
underlying stream is closed.
file
- the file to open for input, must not be null
encoding
- the encoding to use, null
means platform default
- an Iterator of the lines in the file, never
null
listFiles
public static Collection listFiles(File directory,
String[] extensions,
boolean recursive)
Finds files within a given directory (and optionally its subdirectories)
which match an array of extensions.
directory
- the directory to search inextensions
- an array of extensions, ex. {"java","xml"}. If this
parameter is null
, all files are returned.recursive
- if true all subdirectories are searched as well
- an collection of java.io.File with the matching files
listFiles
public static Collection listFiles(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Finds files within a given directory (and optionally its
subdirectories). All files found are filtered by an IOFileFilter.
If your search should recurse into subdirectories you can pass in
an IOFileFilter for directories. You don't need to bind a
DirectoryFileFilter (via logical AND) to this filter. This method does
that for you.
An example: If you want to search through all directories called
"temp" you pass in
FileFilterUtils.NameFileFilter("temp")
Another common usage of this method is find files in a directory
tree but ignoring the directories generated CVS. You can simply pass
in
FileFilterUtils.makeCVSAware(null)
.
directory
- the directory to search infileFilter
- filter to apply when finding files.dirFilter
- optional filter to apply when finding subdirectories.
If this parameter is null
, subdirectories will not be included in the
search. Use TrueFileFilter.INSTANCE to match all directories.
- an collection of java.io.File with the matching files
openInputStream
public static FileInputStream openInputStream(File file)
throws IOException
Opens a
FileInputStream
for the specified file, providing better
error messages than simply calling
new FileInputStream(file)
.
At the end of the method either the stream will be successfully opened,
or an exception will have been thrown.
An exception is thrown if the file does not exist.
An exception is thrown if the file object exists but is a directory.
An exception is thrown if the file exists but cannot be read.
file
- the file to open for input, must not be null
- a new
FileInputStream
for the specified file
openOutputStream
public static FileOutputStream openOutputStream(File file)
throws IOException
Opens a
FileOutputStream
for the specified file, checking and
creating the parent directory if it does not exist.
At the end of the method either the stream will be successfully opened,
or an exception will have been thrown.
The parent directory will be created if it does not exist.
The file will be created if it does not exist.
An exception is thrown if the file object exists but is a directory.
An exception is thrown if the file exists but cannot be written to.
An exception is thrown if the parent directory cannot be created.
file
- the file to open for output, must not be null
- a new
FileOutputStream
for the specified file
readFileToByteArray
public static byte[] readFileToByteArray(File file)
throws IOException
Reads the contents of a file into a byte array.
The file is always closed.
file
- the file to read, must not be null
- the file contents, never
null
readFileToString
public static String readFileToString(File file)
throws IOException
Reads the contents of a file into a String using the default encoding for the VM.
The file is always closed.
file
- the file to read, must not be null
- the file contents, never
null
readFileToString
public static String readFileToString(File file,
String encoding)
throws IOException
Reads the contents of a file into a String.
The file is always closed.
file
- the file to read, must not be null
encoding
- the encoding to use, null
means platform default
- the file contents, never
null
readLines
public static List readLines(File file)
throws IOException
Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.
The file is always closed.
file
- the file to read, must not be null
- the list of Strings representing each line in the file, never
null
readLines
public static List readLines(File file,
String encoding)
throws IOException
Reads the contents of a file line by line to a List of Strings.
The file is always closed.
file
- the file to read, must not be null
encoding
- the encoding to use, null
means platform default
- the list of Strings representing each line in the file, never
null
sizeOfDirectory
public static long sizeOfDirectory(File directory)
Recursively count size of a directory (sum of the length of all files).
directory
- directory to inspect, must not be null
- size of directory in bytes, 0 if directory is security restricted
toFile
public static File toFile(URL url)
Convert from a
URL
to a
File
.
From version 1.1 this method will decode the URL.
Syntax such as
file:///my%20docs/file.txt
will be
correctly decoded to
/my docs/file.txt
.
url
- the file URL to convert, null
returns null
- the equivalent
File
object, or null
if the URL's protocol is not file
toFiles
public static File[] toFiles(URL[] urls)
Converts each of an array of
URL
to a
File
.
Returns an array of the same size as the input.
If the input is
null
, an empty array is returned.
If the input contains
null
, the output array contains
null
at the same
index.
This method will decode the URL.
Syntax such as
file:///my%20docs/file.txt
will be
correctly decoded to
/my docs/file.txt
.
urls
- the file URLs to convert, null
returns empty array
- a non-
null
array of Files matching the input, with a null
item
if there was a null
at that index in the input array
toSuffixes
private static String[] toSuffixes(String[] extensions)
Converts an array of file extensions to suffixes for use
with IOFileFilters.
extensions
- an array of extensions. Format: {"java", "xml"}
- an array of suffixes. Format: {".java", ".xml"}
toURLs
public static URL[] toURLs(File[] files)
throws IOException
Converts each of an array of
File
to a
URL
.
Returns an array of the same size as the input.
files
- the files to convert
- an array of URLs matching the input
touch
public static void touch(File file)
throws IOException
Implements the same behaviour as the "touch" utility on Unix. It creates
a new file with size 0 or, if the file exists already, it is opened and
closed without modifying it, but updating the file date and time.
NOTE: As from v1.3, this method throws an IOException if the last
modified date of the file cannot be set. Also, as from v1.3 this method
creates parent directories if they do not exist.
waitFor
public static boolean waitFor(File file,
int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.
This method repeatedly tests
File.exists()
until it returns
true up to the maximum time specified in seconds.
file
- the file to check, must not be null
seconds
- the maximum time in seconds to wait
writeByteArrayToFile
public static void writeByteArrayToFile(File file,
byte[] data)
throws IOException
Writes a byte array to a file creating the file if it does not exist.
NOTE: As from v1.3, the parent directories of the file will be created
if they do not exist.
file
- the file to write todata
- the content to write to the file
writeLines
public static void writeLines(File file,
Collection lines)
throws IOException
Writes the toString()
value of each item in a collection to
the specified File
line by line.
The default VM encoding and the default line ending will be used.
file
- the file to write tolines
- the lines to write, null
entries produce blank lines
writeLines
public static void writeLines(File file,
Collection lines,
String lineEnding)
throws IOException
Writes the toString()
value of each item in a collection to
the specified File
line by line.
The default VM encoding and the specified line ending will be used.
file
- the file to write tolines
- the lines to write, null
entries produce blank lineslineEnding
- the line separator to use, null
is system default
writeLines
public static void writeLines(File file,
String encoding,
Collection lines)
throws IOException
Writes the
toString()
value of each item in a collection to
the specified
File
line by line.
The specified character encoding and the default line ending will be used.
NOTE: As from v1.3, the parent directories of the file will be created
if they do not exist.
file
- the file to write toencoding
- the encoding to use, null
means platform defaultlines
- the lines to write, null
entries produce blank lines
writeLines
public static void writeLines(File file,
String encoding,
Collection lines,
String lineEnding)
throws IOException
Writes the
toString()
value of each item in a collection to
the specified
File
line by line.
The specified character encoding and the line ending will be used.
NOTE: As from v1.3, the parent directories of the file will be created
if they do not exist.
file
- the file to write toencoding
- the encoding to use, null
means platform defaultlines
- the lines to write, null
entries produce blank lineslineEnding
- the line separator to use, null
is system default
writeStringToFile
public static void writeStringToFile(File file,
String data)
throws IOException
Writes a String to a file creating the file if it does not exist using the default encoding for the VM.
file
- the file to writedata
- the content to write to the file
writeStringToFile
public static void writeStringToFile(File file,
String data,
String encoding)
throws IOException
Writes a String to a file creating the file if it does not exist.
NOTE: As from v1.3, the parent directories of the file will be created
if they do not exist.
file
- the file to writedata
- the content to write to the fileencoding
- the encoding to use, null
means platform default