Class FileUtilsV2_2
- java.lang.Object
-
- org.zeroturnaround.zip.commons.FileUtilsV2_2
-
- Direct Known Subclasses:
FileUtils
public class FileUtilsV2_2 extends java.lang.Object
This is a class that has been made significantly smaller (deleted a bunch of methods) and originally is from the Apache Commons IO 2.2 package (the latest version that supports Java 1.5). All license and other documentation is intact. General file manipulation utilities.Facilities are provided in the following areas:
- writing to a file
- reading from a file
- make a directory including parent directories
- copying files and directories
- deleting files and directories
- converting to and from a URL
- listing files and directories by filter and extension
- comparing file content
- file last changed date
- calculating a checksum
Origin of code: Excalibur, Alexandria, Commons-Utils
- Version:
- $Id: FileUtils.java 1304052 2012-03-22 20:55:29Z ggregory $
-
-
Field Summary
Fields Modifier and Type Field Description static java.io.File[]
EMPTY_FILE_ARRAY
An empty array of typeFile
.private static long
FILE_COPY_BUFFER_SIZE
The file copy buffer size (30 MB)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 java.math.BigInteger
ONE_YB
The number of bytes in a yottabyte.static java.math.BigInteger
ONE_ZB
The number of bytes in a zettabyte.
-
Constructor Summary
Constructors Constructor Description FileUtilsV2_2()
Instances should NOT be constructed in standard programming.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
cleanDirectory(java.io.File directory)
Cleans a directory without deleting it.private static void
cleanDirectoryOnExit(java.io.File directory)
Cleans a directory without deleting it.static boolean
contentEquals(java.io.File file1, java.io.File file2)
Compares the contents of two files to determine if they are equal or not.static void
copyDirectory(java.io.File srcDir, java.io.File destDir)
Copies a whole directory to a new location preserving the file dates.static void
copyDirectory(java.io.File srcDir, java.io.File destDir, boolean preserveFileDate)
Copies a whole directory to a new location.static void
copyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter, boolean preserveFileDate)
Copies a filtered directory to a new location.static void
copyFile(java.io.File srcFile, java.io.File destFile)
Copies a file to a new location preserving the file date.static void
copyFile(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate)
Copies a file to a new location.static void
copyFileToDirectory(java.io.File srcFile, java.io.File destDir)
Copies a file to a directory preserving the file date.static void
copyFileToDirectory(java.io.File srcFile, java.io.File destDir, boolean preserveFileDate)
Copies a file to a directory optionally preserving the file date.static void
deleteDirectory(java.io.File directory)
Deletes a directory recursively.private static void
deleteDirectoryOnExit(java.io.File directory)
Schedules a directory recursively for deletion on JVM exit.static boolean
deleteQuietly(java.io.File file)
Deletes a file, never throwing an exception.private static void
doCopyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter, boolean preserveFileDate, java.util.List<java.lang.String> exclusionList)
Internal copy directory method.private static void
doCopyFile(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate)
Internal copy file method.static void
forceDelete(java.io.File file)
Deletes a file.static void
forceDeleteOnExit(java.io.File file)
Schedules a file to be deleted when JVM exits.static void
forceMkdir(java.io.File directory)
Makes a directory, including any necessary but nonexistent parent directories.static boolean
isSymlink(java.io.File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.static void
moveDirectory(java.io.File srcDir, java.io.File destDir)
Moves a directory.static void
moveFile(java.io.File srcFile, java.io.File destFile)
Moves a file.static java.io.FileInputStream
openInputStream(java.io.File file)
Opens aFileInputStream
for the specified file, providing better error messages than simply callingnew FileInputStream(file)
.static java.io.FileOutputStream
openOutputStream(java.io.File file)
Opens aFileOutputStream
for the specified file, checking and creating the parent directory if it does not exist.static java.io.FileOutputStream
openOutputStream(java.io.File file, boolean append)
Opens aFileOutputStream
for the specified file, checking and creating the parent directory if it does not exist.static java.lang.String
readFileToString(java.io.File file)
Reads the contents of a file into a String using the default encoding for the VM.static java.lang.String
readFileToString(java.io.File file, java.lang.String encoding)
Reads the contents of a file into a String.static long
sizeOf(java.io.File file)
Returns the size of the specified file or directory.static long
sizeOfDirectory(java.io.File directory)
Counts the size of a directory recursively (sum of the length of all files).
-
-
-
Field Detail
-
ONE_KB
public static final long ONE_KB
The number of bytes in a kilobyte.- See Also:
- Constant Field Values
-
ONE_MB
public static final long ONE_MB
The number of bytes in a megabyte.- See Also:
- Constant Field Values
-
FILE_COPY_BUFFER_SIZE
private static final long FILE_COPY_BUFFER_SIZE
The file copy buffer size (30 MB)- See Also:
- Constant Field Values
-
ONE_GB
public static final long ONE_GB
The number of bytes in a gigabyte.- See Also:
- Constant Field Values
-
ONE_TB
public static final long ONE_TB
The number of bytes in a terabyte.- See Also:
- Constant Field Values
-
ONE_PB
public static final long ONE_PB
The number of bytes in a petabyte.- See Also:
- Constant Field Values
-
ONE_EB
public static final long ONE_EB
The number of bytes in an exabyte.- See Also:
- Constant Field Values
-
ONE_ZB
public static final java.math.BigInteger ONE_ZB
The number of bytes in a zettabyte.
-
ONE_YB
public static final java.math.BigInteger ONE_YB
The number of bytes in a yottabyte.
-
EMPTY_FILE_ARRAY
public static final java.io.File[] EMPTY_FILE_ARRAY
An empty array of typeFile
.
-
-
Method Detail
-
openInputStream
public static java.io.FileInputStream openInputStream(java.io.File file) throws java.io.IOException
Opens aFileInputStream
for the specified file, providing better error messages than simply callingnew 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.
- Parameters:
file
- the file to open for input, must not benull
- Returns:
- a new
FileInputStream
for the specified file - Throws:
java.io.FileNotFoundException
- if the file does not existjava.io.IOException
- if the file object is a directoryjava.io.IOException
- if the file cannot be read- Since:
- 1.3
-
openOutputStream
public static java.io.FileOutputStream openOutputStream(java.io.File file) throws java.io.IOException
Opens aFileOutputStream
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.
- Parameters:
file
- the file to open for output, must not benull
- Returns:
- a new
FileOutputStream
for the specified file - Throws:
java.io.IOException
- if the file object is a directoryjava.io.IOException
- if the file cannot be written tojava.io.IOException
- if a parent directory needs creating but that fails- Since:
- 1.3
-
openOutputStream
public static java.io.FileOutputStream openOutputStream(java.io.File file, boolean append) throws java.io.IOException
Opens aFileOutputStream
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.
- Parameters:
file
- the file to open for output, must not benull
append
- iftrue
, then bytes will be added to the end of the file rather than overwriting- Returns:
- a new
FileOutputStream
for the specified file - Throws:
java.io.IOException
- if the file object is a directoryjava.io.IOException
- if the file cannot be written tojava.io.IOException
- if a parent directory needs creating but that fails- Since:
- 2.1
-
contentEquals
public static boolean contentEquals(java.io.File file1, java.io.File file2) throws java.io.IOException
Compares 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
- Parameters:
file1
- the first filefile2
- the second file- Returns:
- true if the content of the files are equal or they both don't exist, false otherwise
- Throws:
java.io.IOException
- in case of an I/O error
-
copyFileToDirectory
public static void copyFileToDirectory(java.io.File srcFile, java.io.File destDir) throws java.io.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.
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.- Parameters:
srcFile
- an existing file to copy, must not benull
destDir
- the directory to place the copy in, must not benull
- Throws:
java.lang.NullPointerException
- if source or destination is nulljava.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs during copying- See Also:
copyFile(File, File, boolean)
-
copyFileToDirectory
public static void copyFileToDirectory(java.io.File srcFile, java.io.File destDir, boolean preserveFileDate) throws java.io.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.
Note: Setting
preserveFileDate
totrue
tries to preserve the file's last modified date/times usingFile.setLastModified(long)
, however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcFile
- an existing file to copy, must not benull
destDir
- the directory to place the copy in, must not benull
preserveFileDate
- true if the file date of the copy should be the same as the original- Throws:
java.lang.NullPointerException
- if source or destination isnull
java.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs during copying- Since:
- 1.3
- See Also:
copyFile(File, File, boolean)
-
copyFile
public static void copyFile(java.io.File srcFile, java.io.File destFile) throws java.io.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.
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.- Parameters:
srcFile
- an existing file to copy, must not benull
destFile
- the new file, must not benull
- Throws:
java.lang.NullPointerException
- if source or destination isnull
java.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs during copying- See Also:
copyFileToDirectory(File, File)
-
copyFile
public static void copyFile(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate) throws java.io.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.
Note: Setting
preserveFileDate
totrue
tries to preserve the file's last modified date/times usingFile.setLastModified(long)
, however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcFile
- an existing file to copy, must not benull
destFile
- the new file, must not benull
preserveFileDate
- true if the file date of the copy should be the same as the original- Throws:
java.lang.NullPointerException
- if source or destination isnull
java.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs during copying- See Also:
copyFileToDirectory(File, File, boolean)
-
doCopyFile
private static void doCopyFile(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate) throws java.io.IOException
Internal copy file method.- Parameters:
srcFile
- the validated source file, must not benull
destFile
- the validated destination file, must not benull
preserveFileDate
- whether to preserve the file date- Throws:
java.io.IOException
- if an error occurs
-
copyDirectory
public static void copyDirectory(java.io.File srcDir, java.io.File destDir) throws java.io.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.
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.- Parameters:
srcDir
- an existing directory to copy, must not benull
destDir
- the new directory, must not benull
- Throws:
java.lang.NullPointerException
- if source or destination isnull
java.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs during copying- Since:
- 1.1
-
copyDirectory
public static void copyDirectory(java.io.File srcDir, java.io.File destDir, boolean preserveFileDate) throws java.io.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.
Note: Setting
preserveFileDate
totrue
tries to preserve the files' last modified date/times usingFile.setLastModified(long)
, however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.- Parameters:
srcDir
- an existing directory to copy, must not benull
destDir
- the new directory, must not benull
preserveFileDate
- true if the file date of the copy should be the same as the original- Throws:
java.lang.NullPointerException
- if source or destination isnull
java.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs during copying- Since:
- 1.1
-
copyDirectory
public static void copyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter, boolean preserveFileDate) throws java.io.IOException
Copies a filtered 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.
Note: Setting
preserveFileDate
totrue
tries to preserve the files' last modified date/times usingFile.setLastModified(long)
, however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.Example: Copy directories only
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY, false);
Example: Copy directories and txt files
// 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);
- Parameters:
srcDir
- an existing directory to copy, must not benull
destDir
- the new directory, must not benull
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 original- Throws:
java.lang.NullPointerException
- if source or destination isnull
java.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs during copying- Since:
- 1.4
-
doCopyDirectory
private static void doCopyDirectory(java.io.File srcDir, java.io.File destDir, java.io.FileFilter filter, boolean preserveFileDate, java.util.List<java.lang.String> exclusionList) throws java.io.IOException
Internal copy directory method.- Parameters:
srcDir
- the validated source directory, must not benull
destDir
- the validated destination directory, must not benull
filter
- the filter to apply, null means copy all directories and filespreserveFileDate
- whether to preserve the file dateexclusionList
- List of files and directories to exclude from the copy, may be null- Throws:
java.io.IOException
- if an error occurs- Since:
- 1.1
-
deleteDirectory
public static void deleteDirectory(java.io.File directory) throws java.io.IOException
Deletes a directory recursively.- Parameters:
directory
- directory to delete- Throws:
java.io.IOException
- in case deletion is unsuccessful
-
deleteQuietly
public static boolean deleteQuietly(java.io.File file)
Deletes a file, never throwing an exception. 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.
- No exceptions are thrown when a file or directory cannot be deleted.
- Parameters:
file
- file or directory to delete, can benull
- Returns:
true
if the file or directory was deleted, otherwisefalse
- Since:
- 1.4
-
cleanDirectory
public static void cleanDirectory(java.io.File directory) throws java.io.IOException
Cleans a directory without deleting it.- Parameters:
directory
- directory to clean- Throws:
java.io.IOException
- in case cleaning is unsuccessful
-
readFileToString
public static java.lang.String readFileToString(java.io.File file, java.lang.String encoding) throws java.io.IOException
Reads the contents of a file into a String. The file is always closed.- Parameters:
file
- the file to read, must not benull
encoding
- the encoding to use,null
means platform default- Returns:
- the file contents, never
null
- Throws:
java.io.IOException
- in case of an I/O errorjava.io.UnsupportedEncodingException
- if the encoding is not supported by the VM
-
readFileToString
public static java.lang.String readFileToString(java.io.File file) throws java.io.IOException
Reads the contents of a file into a String using the default encoding for the VM. The file is always closed.- Parameters:
file
- the file to read, must not benull
- Returns:
- the file contents, never
null
- Throws:
java.io.IOException
- in case of an I/O error- Since:
- 1.3.1
-
forceDelete
public static void forceDelete(java.io.File file) throws java.io.IOException
Deletes 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)
- Parameters:
file
- file or directory to delete, must not benull
- Throws:
java.lang.NullPointerException
- if the directory isnull
java.io.FileNotFoundException
- if the file was not foundjava.io.IOException
- in case deletion is unsuccessful
-
forceDeleteOnExit
public static void forceDeleteOnExit(java.io.File file) throws java.io.IOException
Schedules a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.- Parameters:
file
- file or directory to delete, must not benull
- Throws:
java.lang.NullPointerException
- if the file isnull
java.io.IOException
- in case deletion is unsuccessful
-
deleteDirectoryOnExit
private static void deleteDirectoryOnExit(java.io.File directory) throws java.io.IOException
Schedules a directory recursively for deletion on JVM exit.- Parameters:
directory
- directory to delete, must not benull
- Throws:
java.lang.NullPointerException
- if the directory isnull
java.io.IOException
- in case deletion is unsuccessful
-
cleanDirectoryOnExit
private static void cleanDirectoryOnExit(java.io.File directory) throws java.io.IOException
Cleans a directory without deleting it.- Parameters:
directory
- directory to clean, must not benull
- Throws:
java.lang.NullPointerException
- if the directory isnull
java.io.IOException
- in case cleaning is unsuccessful
-
forceMkdir
public static void forceMkdir(java.io.File directory) throws java.io.IOException
Makes a directory, including any necessary but nonexistent parent directories. If a file already exists with specified name but it is not a directory then an IOException is thrown. If the directory cannot be created (or does not already exist) then an IOException is thrown.- Parameters:
directory
- directory to create, must not benull
- Throws:
java.lang.NullPointerException
- if the directory isnull
java.io.IOException
- if the directory cannot be created or the file already exists but is not a directory
-
sizeOf
public static long sizeOf(java.io.File file)
Returns the size of the specified file or directory. If the providedFile
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.- Parameters:
file
- the regular file or directory to return the size of (must not benull
).- Returns:
- the length of the file, or recursive size of the directory, provided (in bytes).
- Throws:
java.lang.NullPointerException
- if the file isnull
java.lang.IllegalArgumentException
- if the file does not exist.- Since:
- 2.0
-
sizeOfDirectory
public static long sizeOfDirectory(java.io.File directory)
Counts the size of a directory recursively (sum of the length of all files).- Parameters:
directory
- directory to inspect, must not benull
- Returns:
- size of directory in bytes, 0 if directory is security restricted
- Throws:
java.lang.NullPointerException
- if the directory isnull
-
moveDirectory
public static void moveDirectory(java.io.File srcDir, java.io.File destDir) throws java.io.IOException
Moves a directory.When the destination directory is on another file system, do a "copy and delete".
- Parameters:
srcDir
- the directory to be moveddestDir
- the destination directory- Throws:
java.lang.NullPointerException
- if source or destination isnull
FileExistsException
- if the destination directory existsjava.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs moving the file- Since:
- 1.4
-
moveFile
public static void moveFile(java.io.File srcFile, java.io.File destFile) throws java.io.IOException
Moves a file.When the destination file is on another file system, do a "copy and delete".
- Parameters:
srcFile
- the file to be moveddestFile
- the destination file- Throws:
java.lang.NullPointerException
- if source or destination isnull
FileExistsException
- if the destination file existsjava.io.IOException
- if source or destination is invalidjava.io.IOException
- if an IO error occurs moving the file- Since:
- 1.4
-
isSymlink
public static boolean isSymlink(java.io.File file) throws java.io.IOException
Determines whether the specified file is a Symbolic Link rather than an actual file.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 usingFilenameUtils.isSystemWindows()
- Parameters:
file
- the file to check- Returns:
- true if the file is a Symbolic Link
- Throws:
java.io.IOException
- if an IO error occurs while checking the file- Since:
- 2.0
-
-