public class FileUtils extends Object
Facilities are provided in the following areas:
Origin of code: Excalibur, Alexandria, Commons-Utils
| Modifier and Type | Field and Description |
|---|---|
static File[] |
EMPTY_FILE_ARRAY
An empty array of type
File. |
static long |
ONE_GB
The number of bytes in a gigabyte.
|
static long |
ONE_KB
The number of bytes in a kilobyte.
|
static long |
ONE_MB
The number of bytes in a megabyte.
|
| Constructor and Description |
|---|
FileUtils()
Instances should NOT be constructed in standard programming.
|
| Modifier and Type | Method and Description |
|---|---|
static String |
byteCountToDisplaySize(long size)
Returns a human-readable version of the file size, where the input
represents a specific number of bytes.
|
static void |
cleanDirectory(File directory)
Clean a directory without deleting it.
|
static boolean |
contentEquals(File file1,
File file2)
Compare the contents of two files to determine if they are equal or not.
|
static File[] |
convertFileCollectionToFileArray(Collection<?> files)
Converts a Collection containing java.io.File instanced into array
representation.
|
static void |
copyDirectory(File srcDir,
File destDir)
Copies a whole directory to a new location preserving the file dates.
|
static void |
copyDirectory(File srcDir,
File destDir,
boolean preserveFileDate)
Copies a whole directory to a new location.
|
static void |
copyDirectoryToDirectory(File srcDir,
File destDir)
Copies a directory to within another directory preserving the file dates.
|
static void |
copyFile(File srcFile,
File destFile)
Copies a file to a new location preserving the file date.
|
static void |
copyFile(File srcFile,
File destFile,
boolean preserveFileDate)
Copies a file to a new location.
|
static void |
copyFileToDirectory(File srcFile,
File destDir)
Copies a file to a directory preserving the file date.
|
static void |
copyURLToFile(URL source,
File destination)
Copies bytes from the URL
source to a file
destination. |
static void |
deleteDirectory(File directory)
Recursively delete a directory.
|
static void |
forceDelete(File file)
Delete a file.
|
static void |
forceDeleteOnExit(File file)
Schedule a file to be deleted when JVM exits.
|
static void |
forceMkdir(File directory)
Make a directory, including any necessary but nonexistent parent
directories.
|
static boolean |
isFileNewer(File file,
Date date)
Tests if the specified
File is newer than the specified
Date. |
static boolean |
isFileNewer(File file,
File reference)
Tests if the specified
File is newer than the reference
File. |
static boolean |
isFileNewer(File file,
long timeMillis)
Tests if the specified
File is newer than the specified
time reference. |
static String |
readFileToString(File file,
String encoding)
Reads the contents of a file into a String.
|
static List |
readLines(File file,
String encoding)
Reads the contents of a file line by line to a List of Strings.
|
static long |
sizeOfDirectory(File directory)
Recursively count size of a directory (sum of the length of all files).
|
static File |
toFile(URL url)
Convert from a
URL to a File. |
static File[] |
toFiles(URL[] urls)
Converts each of an array of
URL to a File. |
static void |
touch(File file)
Implements the same behaviour as the "touch" utility on Unix.
|
static URL[] |
toURLs(File[] files)
Converts each of an array of
File to a URL. |
static boolean |
waitFor(File file,
int seconds)
Waits for NFS to propagate a file creation, imposing a timeout.
|
static void |
writeByteArrayToFile(File file,
byte[] data)
Writes a byte array to a file creating the file if it does not exist.
|
static void |
writeLines(File file,
String encoding,
Collection<?> lines)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
String encoding,
Collection<?> lines,
String lineEnding)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeStringToFile(File file,
String data,
String encoding)
Writes a String to a file creating the file if it does not exist.
|
public static final long ONE_KB
public static final long ONE_MB
public static final long ONE_GB
public static final File[] EMPTY_FILE_ARRAY
File.public FileUtils()
public static String byteCountToDisplaySize(long size)
size - the number of bytespublic static void touch(File file) throws IOException
file - the File to touchIOException - If an I/O problem occurspublic static File[] convertFileCollectionToFileArray(Collection<?> files)
files - a Collection containing java.io.File instancespublic static boolean contentEquals(File file1, File file2) throws IOException
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 fileIOException - in case of an I/O errorpublic static File toFile(URL url)
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 nullFile object, or null
if the URL's protocol is not fileIllegalArgumentException - if the file is incorrectly encodedpublic static File[] toFiles(URL[] urls)
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 arrayIllegalArgumentException - if any file is not a URL fileIllegalArgumentException - if any file is incorrectly encodedpublic static URL[] toURLs(File[] files) throws IOException
File to a URL.
Returns an array of the same size as the input.
files - the files to convertIOException - if a file cannot be convertedpublic static void copyFileToDirectory(File srcFile, File destDir) throws IOException
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 nulldestDir - the directory to place the copy in, must not be nullNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingcopyFile(File, File, boolean)public static void copyFile(File srcFile, File destFile) throws IOException
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 nulldestFile - the new file, must not be nullNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingcopyFileToDirectory(java.io.File, java.io.File)public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException
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 nulldestFile - the new file, must not be nullpreserveFileDate - true if the file date of the copy
should be the same as the originalNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingcopyFileToDirectory(java.io.File, java.io.File)public static void copyDirectoryToDirectory(File srcDir, File destDir) throws IOException
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 nulldestDir - the directory to place the copy in, must not be nullNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingpublic static void copyDirectory(File srcDir, File destDir) throws IOException
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 nulldestDir - the new directory, must not be nullNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingpublic static void copyDirectory(File srcDir, File destDir, boolean preserveFileDate) throws IOException
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 nulldestDir - the new directory, must not be nullpreserveFileDate - true if the file date of the copy
should be the same as the originalNullPointerException - if source or destination is nullIOException - if source or destination is invalidIOException - if an IO error occurs during copyingpublic static void copyURLToFile(URL source, File destination) throws IOException
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 - A URL to copy bytes from.destination - A non-directory File to write bytes to
(possibly overwriting).IOException - if
source URL cannot be openeddestination cannot be written topublic static void deleteDirectory(File directory) throws IOException
directory - directory to deleteIOException - in case deletion is unsuccessfulpublic static void cleanDirectory(File directory) throws IOException
directory - directory to cleanIOException - in case cleaning is unsuccessfulpublic static boolean waitFor(File file, int seconds)
This method repeatedly tests File.exists() until it returns
true up to the maximum time specified in seconds.
file - the file to check, not nullseconds - the maximum time in seconds to waitNullPointerException - if the file is nullpublic static String readFileToString(File file, String encoding) throws IOException
There is no readFileToString method without encoding parameter because the default encoding can differ between platforms and will have inconsistent results.
file - the file to readencoding - the encoding to use, null means platform defaultIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static List readLines(File file, String encoding) throws IOException
There is no readLines method without encoding parameter because the default encoding can differ between platforms and will have inconsistent results.
file - the file to readencoding - the encoding to use, null means platform defaultIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeStringToFile(File file, String data, String encoding) throws IOException
There is no writeStringToFile method without encoding parameter because the default encoding can differ between platforms and will have inconsistent results.
file - the file to writedata - the content to write to the fileencoding - the encoding to use, null means platform defaultIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeByteArrayToFile(File file, byte[] data) throws IOException
file - the file to write todata - the content to write to the fileIOException - in case of an I/O errorpublic static void writeLines(File file, String encoding, Collection<?> lines) throws IOException
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.
There is no writeLines method without encoding parameter because the default encoding can differ between platforms and will have inconsistent results.
file - the file to write toencoding - the encoding to use, null means platform defaultlines - the lines to write, null entries produce blank linesIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void writeLines(File file, String encoding, Collection<?> lines, String lineEnding) throws IOException
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.
There is no writeLines method without encoding parameter because the default encoding can differ between platforms and will have inconsistent results.
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 defaultIOException - in case of an I/O errorUnsupportedEncodingException - if the encoding is not supported by the VMpublic static void forceDelete(File file) throws IOException
The difference between File.delete() and this method are:
file - file or directory to delete, not nullNullPointerException - if the directory is nullIOException - in case deletion is unsuccessfulpublic static void forceDeleteOnExit(File file) throws IOException
file - file or directory to delete, not nullNullPointerException - if the file is nullIOException - in case deletion is unsuccessfulpublic static void forceMkdir(File directory) throws IOException
directory - directory to create, not nullNullPointerException - if the directory is nullIOException - if the directory cannot be createdpublic static long sizeOfDirectory(File directory)
directory - directory to inspect, not nullNullPointerException - if the directory is nullpublic static boolean isFileNewer(File file, File reference)
File is newer than the reference
File.file - the File of which the modification date must
be compared, not nullreference - the File of which the modification date
is used, not nullFile exists and has been modified more
recently than the reference FileIllegalArgumentException - if the file is nullIllegalArgumentException - if the reference file is null or doesn't existpublic static boolean isFileNewer(File file, Date date)
File is newer than the specified
Date.file - the File of which the modification date
must be compared, not nulldate - the date reference, not nullFile exists and has been modified
after the given Date.IllegalArgumentException - if the file is nullIllegalArgumentException - if the date is nullpublic static boolean isFileNewer(File file, long timeMillis)
File is newer than the specified
time reference.file - the File of which the modification date must
be compared, not nulltimeMillis - the time reference measured in milliseconds since the
epoch (00:00:00 GMT, January 1, 1970)File exists and has been modified after
the given time reference.IllegalArgumentException - if the file is nullCopyright © 2016. All Rights Reserved.