public class FileUtilsV2_2 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_EB
The number of bytes in an exabyte.
|
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.
|
static long |
ONE_PB
The number of bytes in a petabyte.
|
static long |
ONE_TB
The number of bytes in a terabyte.
|
static BigInteger |
ONE_YB
The number of bytes in a yottabyte.
|
static BigInteger |
ONE_ZB
The number of bytes in a zettabyte.
|
Constructor and Description |
---|
FileUtilsV2_2()
Instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static void |
cleanDirectory(File directory)
Cleans a directory without deleting it.
|
static boolean |
contentEquals(File file1,
File file2)
Compares the contents of two files to determine if they are equal or not.
|
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 |
copyDirectory(File srcDir,
File destDir,
FileFilter filter,
boolean preserveFileDate)
Copies a filtered directory to a new location.
|
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 |
copyFileToDirectory(File srcFile,
File destDir,
boolean preserveFileDate)
Copies a file to a directory optionally preserving the file date.
|
static void |
deleteDirectory(File directory)
Deletes a directory recursively.
|
static boolean |
deleteQuietly(File file)
Deletes a file, never throwing an exception.
|
static void |
forceDelete(File file)
Deletes a file.
|
static void |
forceDeleteOnExit(File file)
Schedules a file to be deleted when JVM exits.
|
static void |
forceMkdir(File directory)
Makes a directory, including any necessary but nonexistent parent
directories.
|
static boolean |
isSymlink(File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.
|
static void |
moveDirectory(File srcDir,
File destDir)
Moves a directory.
|
static void |
moveFile(File srcFile,
File destFile)
Moves a file.
|
static FileInputStream |
openInputStream(File file)
Opens a
FileInputStream for the specified file, providing better
error messages than simply calling new FileInputStream(file) . |
static FileOutputStream |
openOutputStream(File file)
Opens a
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist. |
static FileOutputStream |
openOutputStream(File file,
boolean append)
Opens a
FileOutputStream for the specified file, checking and
creating the parent directory if it does not exist. |
static String |
readFileToString(File file)
Reads the contents of a file into a String using the default encoding for the VM.
|
static String |
readFileToString(File file,
String encoding)
Reads the contents of a file into a String.
|
static long |
sizeOf(File file)
Returns the size of the specified file or directory.
|
static long |
sizeOfDirectory(File directory)
Counts the size of a directory recursively (sum of the length of all files).
|
public static final long ONE_KB
public static final long ONE_MB
public static final long ONE_GB
public static final long ONE_TB
public static final long ONE_PB
public static final long ONE_EB
public static final BigInteger ONE_ZB
public static final BigInteger ONE_YB
public static final File[] EMPTY_FILE_ARRAY
File
.public FileUtilsV2_2()
public static FileInputStream openInputStream(File file) throws IOException
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
FileInputStream
for the specified fileFileNotFoundException
- if the file does not existIOException
- if the file object is a directoryIOException
- if the file cannot be readpublic static FileOutputStream openOutputStream(File file) throws IOException
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
FileOutputStream
for the specified fileIOException
- if the file object is a directoryIOException
- if the file cannot be written toIOException
- if a parent directory needs creating but that failspublic static FileOutputStream openOutputStream(File file, boolean append) throws IOException
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
append
- if true
, then bytes will be added to the
end of the file rather than overwritingFileOutputStream
for the specified fileIOException
- if the file object is a directoryIOException
- if the file cannot be written toIOException
- if a parent directory needs creating but that failspublic 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 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.
Note: This method tries to preserve the file's last
modified date/times using File.setLastModified(long)
, however
it is not guaranteed that the operation will succeed.
If the modification operation fails, no indication is provided.
srcFile
- an existing file to copy, must not be null
destDir
- the directory to place the copy in, must not be null
NullPointerException
- 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 copyFileToDirectory(File srcFile, File destDir, boolean preserveFileDate) 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.
Note: Setting preserveFileDate
to
true
tries to preserve the file's last modified
date/times using File.setLastModified(long)
, however it is
not guaranteed that the operation will succeed.
If the modification operation fails, no indication is provided.
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 originalNullPointerException
- if source or destination is null
IOException
- 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.
Note: This method tries to preserve the file's last
modified date/times using File.setLastModified(long)
, however
it is not guaranteed that the operation will succeed.
If the modification operation fails, no indication is provided.
srcFile
- an existing file to copy, must not be null
destFile
- the new file, must not be null
NullPointerException
- if source or destination is null
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copyingcopyFileToDirectory(File, 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.
Note: Setting preserveFileDate
to
true
tries to preserve the file's last modified
date/times using File.setLastModified(long)
, however it is
not guaranteed that the operation will succeed.
If the modification operation fails, no indication is provided.
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 originalNullPointerException
- if source or destination is null
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copyingcopyFileToDirectory(File, File, boolean)
public 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.
Note: This method tries to preserve the files' last
modified date/times using File.setLastModified(long)
, however
it is not guaranteed that those operations will succeed.
If the modification operation fails, no indication is provided.
srcDir
- an existing directory to copy, must not be null
destDir
- the new directory, must not be null
NullPointerException
- if source or destination is null
IOException
- 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.
Note: Setting preserveFileDate
to
true
tries to preserve the files' last modified
date/times using File.setLastModified(long)
, however it is
not guaranteed that those operations will succeed.
If the modification operation fails, no indication is provided.
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 originalNullPointerException
- if source or destination is null
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copyingpublic static void copyDirectory(File srcDir, File destDir, FileFilter filter, 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.
Note: Setting preserveFileDate
to
true
tries to preserve the files' last modified
date/times using File.setLastModified(long)
, however it is
not guaranteed that those operations will succeed.
If the modification operation fails, no indication is provided.
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
// Create a filter for ".txt" files IOFileFilter txtSuffixFilter = FileFilterUtils.suffixFileFilter(".txt"); IOFileFilter txtFiles = FileFilterUtils.andFileFilter(FileFileFilter.FILE, txtSuffixFilter); // Create a filter for either directories or ".txt" files FileFilter filter = FileFilterUtils.orFileFilter(DirectoryFileFilter.DIRECTORY, txtFiles); // Copy using the filter FileUtils.copyDirectory(srcDir, destDir, filter, false);
srcDir
- an existing directory to copy, must not be null
destDir
- the new directory, must not be null
filter
- the filter to apply, null means copy all directories and filespreserveFileDate
- true if the file date of the copy
should be the same as the originalNullPointerException
- if source or destination is null
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copyingpublic static void deleteDirectory(File directory) throws IOException
directory
- directory to deleteIOException
- in case deletion is unsuccessfulpublic static boolean deleteQuietly(File file)
The difference between File.delete() and this method are:
file
- file or directory to delete, can be null
true
if the file or directory was deleted, otherwise
false
public static void cleanDirectory(File directory) throws IOException
directory
- directory to cleanIOException
- in case cleaning is unsuccessfulpublic static String readFileToString(File file, String encoding) throws IOException
file
- the file to read, must not be null
encoding
- the encoding to use, null
means platform defaultnull
IOException
- in case of an I/O errorUnsupportedEncodingException
- if the encoding is not supported by the VMpublic static String readFileToString(File file) throws IOException
file
- the file to read, must not be null
null
IOException
- in case of an I/O errorpublic static void forceDelete(File file) throws IOException
The difference between File.delete() and this method are:
file
- file or directory to delete, must not be null
NullPointerException
- if the directory is null
FileNotFoundException
- if the file was not foundIOException
- in case deletion is unsuccessfulpublic static void forceDeleteOnExit(File file) throws IOException
file
- file or directory to delete, must not be null
NullPointerException
- if the file is null
IOException
- in case deletion is unsuccessfulpublic static void forceMkdir(File directory) throws IOException
directory
- directory to create, must not be null
NullPointerException
- if the directory is null
IOException
- if the directory cannot be created or the file already exists but is not a directorypublic static long sizeOf(File file)
File
is a regular file, then the file's length is returned.
If the argument is a directory, then the size of the directory is
calculated recursively. If a directory or subdirectory is security
restricted, its size will not be included.file
- the regular file or directory to return the size
of (must not be null
).NullPointerException
- if the file is null
IllegalArgumentException
- if the file does not exist.public static long sizeOfDirectory(File directory)
directory
- directory to inspect, must not be null
NullPointerException
- if the directory is null
public static void moveDirectory(File srcDir, File destDir) throws IOException
When the destination directory is on another file system, do a "copy and delete".
srcDir
- the directory to be moveddestDir
- the destination directoryNullPointerException
- if source or destination is null
FileExistsException
- if the destination directory existsIOException
- if source or destination is invalidIOException
- if an IO error occurs moving the filepublic static void moveFile(File srcFile, File destFile) throws IOException
When the destination file is on another file system, do a "copy and delete".
srcFile
- the file to be moveddestFile
- the destination fileNullPointerException
- if source or destination is null
FileExistsException
- if the destination file existsIOException
- if source or destination is invalidIOException
- if an IO error occurs moving the filepublic static boolean isSymlink(File file) throws IOException
Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.
Note: the current implementation always returns false
if the system
is detected as Windows using FilenameUtils.isSystemWindows()
file
- the file to checkIOException
- if an IO error occurs while checking the fileCopyright © 2017 ZeroTurnaround. All rights reserved.