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_EB
The number of bytes in an exabyte.
|
static BigInteger |
ONE_EB_BI
The number of bytes in an exabyte.
|
static long |
ONE_GB
The number of bytes in a gigabyte.
|
static BigInteger |
ONE_GB_BI
The number of bytes in a gigabyte.
|
static long |
ONE_KB
The number of bytes in a kilobyte.
|
static BigInteger |
ONE_KB_BI
The number of bytes in a kilobyte.
|
static long |
ONE_MB
The number of bytes in a megabyte.
|
static BigInteger |
ONE_MB_BI
The number of bytes in a megabyte.
|
static long |
ONE_PB
The number of bytes in a petabyte.
|
static BigInteger |
ONE_PB_BI
The number of bytes in a petabyte.
|
static long |
ONE_TB
The number of bytes in a terabyte.
|
static BigInteger |
ONE_TB_BI
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 |
---|
FileUtils()
Instances should NOT be constructed in standard programming.
|
Modifier and Type | Method and Description |
---|---|
static String |
byteCountToDisplaySize(BigInteger size)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
|
static String |
byteCountToDisplaySize(long size)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
|
static Checksum |
checksum(File file,
Checksum checksum)
Computes the checksum of a file using the specified checksum object.
|
static long |
checksumCRC32(File file)
Computes the checksum of a file using the CRC32 checksum routine.
|
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 boolean |
contentEqualsIgnoreEOL(File file1,
File file2,
String charsetName)
Compares the contents of two files to determine if they are equal or not.
|
static File[] |
convertFileCollectionToFileArray(Collection<File> 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 |
copyDirectory(File srcDir,
File destDir,
FileFilter filter)
Copies a filtered directory to a new location preserving the file dates.
|
static void |
copyDirectory(File srcDir,
File destDir,
FileFilter filter,
boolean preserveFileDate)
Copies a filtered 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 long |
copyFile(File input,
OutputStream output)
Copy bytes from a
File to an OutputStream . |
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 |
copyInputStreamToFile(InputStream source,
File destination)
|
static void |
copyToFile(InputStream source,
File destination)
|
static void |
copyURLToFile(URL source,
File destination)
Copies bytes from the URL
source to a file
destination . |
static void |
copyURLToFile(URL source,
File destination,
int connectionTimeout,
int readTimeout)
Copies bytes from the URL
source to a file
destination . |
static void |
deleteDirectory(File directory)
Deletes a directory recursively.
|
static boolean |
deleteQuietly(File file)
Deletes a file, never throwing an exception.
|
static boolean |
directoryContains(File directory,
File child)
Determines whether the
parent directory contains the child element (a file or directory). |
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 void |
forceMkdirParent(File file)
Makes any necessary but nonexistent parent directories for a given File.
|
static File |
getFile(File directory,
String... names)
Construct a file from the set of name elements.
|
static File |
getFile(String... names)
Construct a file from the set of name elements.
|
static File |
getTempDirectory()
Returns a
File representing the system temporary directory. |
static String |
getTempDirectoryPath()
Returns the path to the system temporary directory.
|
static File |
getUserDirectory()
Returns a
File representing the user's home directory. |
static String |
getUserDirectoryPath()
Returns the path to the user's home directory.
|
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 boolean |
isFileOlder(File file,
Date date)
Tests if the specified
File is older than the specified
Date . |
static boolean |
isFileOlder(File file,
File reference)
Tests if the specified
File is older than the reference
File . |
static boolean |
isFileOlder(File file,
long timeMillis)
Tests if the specified
File is older than the specified
time reference. |
static boolean |
isSymlink(File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.
|
static Iterator<File> |
iterateFiles(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Allows iteration over the files in given directory (and optionally
its subdirectories).
|
static Iterator<File> |
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.
|
static Iterator<File> |
iterateFilesAndDirs(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Allows iteration over the files in given directory (and optionally
its subdirectories).
|
static LineIterator |
lineIterator(File file)
Returns an Iterator for the lines in a
File using the default encoding for the VM. |
static LineIterator |
lineIterator(File file,
String encoding)
Returns an Iterator for the lines in a
File . |
static Collection<File> |
listFiles(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Finds files within a given directory (and optionally its
subdirectories).
|
static Collection<File> |
listFiles(File directory,
String[] extensions,
boolean recursive)
Finds files within a given directory (and optionally its subdirectories)
which match an array of extensions.
|
static Collection<File> |
listFilesAndDirs(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter)
Finds files within a given directory (and optionally its
subdirectories).
|
static void |
moveDirectory(File srcDir,
File destDir)
Moves a directory.
|
static void |
moveDirectoryToDirectory(File src,
File destDir,
boolean createDestDir)
Moves a directory to another directory.
|
static void |
moveFile(File srcFile,
File destFile)
Moves a file.
|
static void |
moveFileToDirectory(File srcFile,
File destDir,
boolean createDestDir)
Moves a file to a directory.
|
static void |
moveToDirectory(File src,
File destDir,
boolean createDestDir)
Moves a file or directory to the destination directory.
|
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 byte[] |
readFileToByteArray(File file)
Reads the contents of a file into a byte array.
|
static String |
readFileToString(File file)
Deprecated.
2.5 use
readFileToString(File, Charset) instead |
static String |
readFileToString(File file,
Charset encoding)
Reads the contents of a file into a String.
|
static String |
readFileToString(File file,
String encoding)
Reads the contents of a file into a String.
|
static List<String> |
readLines(File file)
Deprecated.
2.5 use
readLines(File, Charset) instead |
static List<String> |
readLines(File file,
Charset encoding)
Reads the contents of a file line by line to a List of Strings.
|
static List<String> |
readLines(File file,
String encoding)
Reads the contents of a file line by line to a List of Strings.
|
static long |
sizeOf(File file)
Returns the size of the specified file or directory.
|
static BigInteger |
sizeOfAsBigInteger(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).
|
static BigInteger |
sizeOfDirectoryAsBigInteger(File directory)
Counts the size of a directory recursively (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 |
write(File file,
CharSequence data)
Deprecated.
2.5 use
write(File, CharSequence, Charset) instead |
static void |
write(File file,
CharSequence data,
boolean append)
Deprecated.
2.5 use
write(File, CharSequence, Charset, boolean) instead |
static void |
write(File file,
CharSequence data,
Charset encoding)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
write(File file,
CharSequence data,
Charset encoding,
boolean append)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
write(File file,
CharSequence data,
String encoding)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
write(File file,
CharSequence data,
String encoding,
boolean append)
Writes a CharSequence to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(File file,
byte[] data)
Writes a byte array to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(File file,
byte[] data,
boolean append)
Writes a byte array to a file creating the file if it does not exist.
|
static void |
writeByteArrayToFile(File file,
byte[] data,
int off,
int len)
Writes
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist. |
static void |
writeByteArrayToFile(File file,
byte[] data,
int off,
int len,
boolean append)
Writes
len bytes from the specified byte array starting
at offset off to a file, creating the file if it does
not exist. |
static void |
writeLines(File file,
Collection<?> lines)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
Collection<?> lines,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
Collection<?> lines,
String lineEnding)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeLines(File file,
Collection<?> lines,
String lineEnding,
boolean append)
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)
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,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line, optionally appending. |
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 |
writeLines(File file,
String encoding,
Collection<?> lines,
String lineEnding,
boolean append)
Writes the
toString() value of each item in a collection to
the specified File line by line. |
static void |
writeStringToFile(File file,
String data)
Deprecated.
2.5 use
writeStringToFile(File, String, Charset) instead |
static void |
writeStringToFile(File file,
String data,
boolean append)
Deprecated.
2.5 use
writeStringToFile(File, String, Charset, boolean) instead |
static void |
writeStringToFile(File file,
String data,
Charset encoding)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(File file,
String data,
Charset encoding,
boolean append)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(File file,
String data,
String encoding)
Writes a String to a file creating the file if it does not exist.
|
static void |
writeStringToFile(File file,
String data,
String encoding,
boolean append)
Writes a String to a file creating the file if it does not exist.
|
public static final long ONE_KB
public static final BigInteger ONE_KB_BI
public static final long ONE_MB
public static final BigInteger ONE_MB_BI
public static final long ONE_GB
public static final BigInteger ONE_GB_BI
public static final long ONE_TB
public static final BigInteger ONE_TB_BI
public static final long ONE_PB
public static final BigInteger ONE_PB_BI
public static final long ONE_EB
public static final BigInteger ONE_EB_BI
public static final BigInteger ONE_ZB
public static final BigInteger ONE_YB
public static final File[] EMPTY_FILE_ARRAY
File
.public FileUtils()
public static File getFile(File directory, String... names)
directory
- the parent directorynames
- the name elementspublic static File getFile(String... names)
names
- the name elementspublic static String getTempDirectoryPath()
public static File getTempDirectory()
File
representing the system temporary directory.public static String getUserDirectoryPath()
public static File getUserDirectory()
File
representing the user's home directory.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 String byteCountToDisplaySize(BigInteger size)
If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the nearest GB boundary.
Similarly for the 1MB and 1KB boundaries.
size
- the number of bytespublic static String byteCountToDisplaySize(long size)
If the size is over 1GB, the size is returned as the number of whole GB, i.e. the size is rounded down to the nearest GB boundary.
Similarly for the 1MB and 1KB boundaries.
size
- the number of bytespublic static void touch(File file) throws IOException
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.
file
- the File to touchIOException
- If an I/O problem occurspublic static File[] convertFileCollectionToFileArray(Collection<File> files)
files
- a Collection containing java.io.File instancespublic static Collection<File> listFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
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.FileFilterUtils
,
NameFileFilter
public static Collection<File> listFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
The resulting collection includes the starting directory and any subdirectories that match the directory filter.
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.listFiles(java.io.File, org.apache.commons.io.filefilter.IOFileFilter, org.apache.commons.io.filefilter.IOFileFilter)
,
FileFilterUtils
,
NameFileFilter
public static Iterator<File> iterateFiles(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
All files found are filtered by an IOFileFilter. This method is
based on listFiles(File, IOFileFilter, IOFileFilter)
,
which supports Iterable ('foreach' loop).
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.FileFilterUtils
,
NameFileFilter
public static Iterator<File> iterateFilesAndDirs(File directory, IOFileFilter fileFilter, IOFileFilter dirFilter)
All files found are filtered by an IOFileFilter. This method is
based on listFilesAndDirs(File, IOFileFilter, IOFileFilter)
,
which supports Iterable ('foreach' loop).
The resulting iterator includes the subdirectories themselves.
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.FileFilterUtils
,
NameFileFilter
public static Collection<File> listFiles(File directory, String[] extensions, boolean recursive)
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 wellpublic static Iterator<File> iterateFiles(File directory, String[] extensions, boolean recursive)
listFiles(File, String[], boolean)
,
which supports Iterable ('foreach' loop).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 wellpublic 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 boolean contentEqualsIgnoreEOL(File file1, File file2, String charsetName) throws IOException
This method checks to see if the two files point to the same file, before resorting to line-by-line comparison of the contents.
file1
- the first filefile2
- the second filecharsetName
- the character encoding to be used.
May be null, in which case the platform default is usedIOException
- in case of an I/O errorIOUtils.contentEqualsIgnoreEOL(Reader, Reader)
public 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
. Starting with version
1.5, this method uses UTF-8 to decode percent-encoded octets to characters.
Additionally, malformed percent-encoded octets are handled leniently by
passing them through literally.
url
- the file URL to convert, null
returns null
File
object, or null
if the URL's protocol is not file
public 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 arraynull
array of Files matching the input, with a null
item
if there was a null
at that index in the input 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 convert, must not be null
IOException
- if a file cannot be convertedNullPointerException
- if the parameter is nullpublic 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 copyingIOException
- if the output file length is not the same as the input file length after the copy
completescopyFile(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 copyingIOException
- if the output file length is not the same as the input file length after the copy
completescopyFileToDirectory(File, File)
,
copyFile(File, File, boolean)
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 copyingIOException
- if the output file length is not the same as the input file length after the copy
completescopyFileToDirectory(File, File, boolean)
,
doCopyFile(File, File, boolean)
public static long copyFile(File input, OutputStream output) throws IOException
File
to an OutputStream
.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.
input
- the File
to read fromoutput
- the OutputStream
to write toNullPointerException
- if the input or output is nullIOException
- if an I/O error occurspublic 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.
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 directory to place the copy in, 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) 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) 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: 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.
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, DirectoryFileFilter.DIRECTORY);
// 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);
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 files
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 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.
Warning: this method does not set a connection or read timeout and thus
might block forever. Use copyURLToFile(URL, File, int, int)
with reasonable timeouts to prevent this.
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
IOException
- if source
URL cannot be openedIOException
- if destination
is a directoryIOException
- if destination
cannot be writtenIOException
- if destination
needs creating but can't beIOException
- if an IO error occurs during copyingpublic static void copyURLToFile(URL source, File destination, int connectionTimeout, int readTimeout) 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
- 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
connectionTimeout
- the number of milliseconds until this method
will timeout if no connection could be established to the source
readTimeout
- the number of milliseconds until this method will
timeout if no data could be read from the source
IOException
- if source
URL cannot be openedIOException
- if destination
is a directoryIOException
- if destination
cannot be writtenIOException
- if destination
needs creating but can't beIOException
- if an IO error occurs during copyingpublic static void copyInputStreamToFile(InputStream source, File destination) throws IOException
InputStream
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.
The source
stream is closed.
See copyToFile(InputStream, File)
for a method that does not close the input stream.source
- the InputStream
to copy bytes from, must not be null
, will be closeddestination
- the non-directory File
to write bytes to
(possibly overwriting), must not be null
IOException
- if destination
is a directoryIOException
- if destination
cannot be writtenIOException
- if destination
needs creating but can't beIOException
- if an IO error occurs during copyingpublic static void copyToFile(InputStream source, File destination) throws IOException
InputStream
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.
The source
stream is left open, e.g. for use with ZipInputStream
.
See copyInputStreamToFile(InputStream, File)
for a method that closes the input stream.source
- the InputStream
to copy bytes from, must not be null
destination
- the non-directory File
to write bytes to
(possibly overwriting), must not be null
IOException
- if destination
is a directoryIOException
- if destination
cannot be writtenIOException
- if destination
needs creating but can't beIOException
- if an IO error occurs during copyingpublic static void deleteDirectory(File directory) throws IOException
directory
- directory to deleteIOException
- in case deletion is unsuccessfulIllegalArgumentException
- if directory
does not exist or is not a directorypublic 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 boolean directoryContains(File directory, File child) throws IOException
parent
directory contains the child
element (a file or directory).
Files are normalized before comparison.
Edge cases:directory
must not be null: if null, throw IllegalArgumentExceptiondirectory
must be a directory: if not a directory, throw IllegalArgumentExceptiondirectory
- the file to consider as the parent.child
- the file to consider as the child.IOException
- if an IO error occurs while checking the files.IllegalArgumentException
- if directory
is null or not a directory.FilenameUtils.directoryContains(String, String)
public static void cleanDirectory(File directory) throws IOException
directory
- directory to cleanIOException
- in case cleaning is unsuccessfulIllegalArgumentException
- if directory
does not exist or is not a directorypublic 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, must not be null
seconds
- the maximum time in seconds to waitNullPointerException
- if the file is null
public static String readFileToString(File file, Charset 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 errorpublic 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 errorUnsupportedCharsetException
- thrown instead of .UnsupportedEncodingException
in version 2.2 if the encoding is not supported.@Deprecated public static String readFileToString(File file) throws IOException
readFileToString(File, Charset)
insteadfile
- the file to read, must not be null
null
IOException
- in case of an I/O errorpublic static byte[] readFileToByteArray(File file) throws IOException
file
- the file to read, must not be null
null
IOException
- in case of an I/O errorpublic static List<String> readLines(File file, Charset 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 errorpublic static List<String> readLines(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 errorUnsupportedCharsetException
- thrown instead of .UnsupportedEncodingException
in version 2.2 if the encoding is not supported.@Deprecated public static List<String> readLines(File file) throws IOException
readLines(File, Charset)
insteadfile
- the file to read, must not be null
null
IOException
- in case of an I/O errorpublic static LineIterator lineIterator(File file, String encoding) throws IOException
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 defaultnull
IOException
- in case of an I/O error (file closed)public static LineIterator lineIterator(File file) throws IOException
File
using the default encoding for the VM.file
- the file to open for input, must not be null
null
IOException
- in case of an I/O error (file closed)lineIterator(File, String)
public static void writeStringToFile(File file, String data, Charset encoding) throws IOException
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 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
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 defaultIOException
- in case of an I/O errorUnsupportedEncodingException
- if the encoding is not supported by the VMpublic static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException
file
- the file to writedata
- the content to write to the fileencoding
- the encoding to use, null
means platform defaultappend
- if true
, then the String will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorpublic static void writeStringToFile(File file, String data, String encoding, boolean append) throws IOException
file
- the file to writedata
- the content to write to the fileencoding
- the encoding to use, null
means platform defaultappend
- if true
, then the String will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorUnsupportedCharsetException
- thrown instead of .UnsupportedEncodingException
in version 2.2 if the encoding is not supported by the VM@Deprecated public static void writeStringToFile(File file, String data) throws IOException
writeStringToFile(File, String, Charset)
insteadfile
- the file to writedata
- the content to write to the fileIOException
- in case of an I/O error@Deprecated public static void writeStringToFile(File file, String data, boolean append) throws IOException
writeStringToFile(File, String, Charset, boolean)
insteadfile
- the file to writedata
- the content to write to the fileappend
- if true
, then the String will be added to the
end of the file rather than overwritingIOException
- in case of an I/O error@Deprecated public static void write(File file, CharSequence data) throws IOException
write(File, CharSequence, Charset)
insteadfile
- the file to writedata
- the content to write to the fileIOException
- in case of an I/O error@Deprecated public static void write(File file, CharSequence data, boolean append) throws IOException
write(File, CharSequence, Charset, boolean)
insteadfile
- the file to writedata
- the content to write to the fileappend
- if true
, then the data will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorpublic static void write(File file, CharSequence data, Charset encoding) throws IOException
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 errorpublic static void write(File file, CharSequence data, String encoding) throws IOException
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 write(File file, CharSequence data, Charset encoding, boolean append) throws IOException
file
- the file to writedata
- the content to write to the fileencoding
- the encoding to use, null
means platform defaultappend
- if true
, then the data will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorpublic static void write(File file, CharSequence data, String encoding, boolean append) throws IOException
file
- the file to writedata
- the content to write to the fileencoding
- the encoding to use, null
means platform defaultappend
- if true
, then the data will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorUnsupportedCharsetException
- thrown instead of .UnsupportedEncodingException
in version 2.2 if the encoding is not supported by the VMpublic static void writeByteArrayToFile(File file, byte[] data) throws IOException
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 fileIOException
- in case of an I/O errorpublic static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException
file
- the file to write todata
- the content to write to the fileappend
- if true
, then bytes will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorpublic static void writeByteArrayToFile(File file, byte[] data, int off, int len) throws IOException
len
bytes from the specified byte array starting
at offset off
to a file, creating the file if it does
not exist.file
- the file to write todata
- the content to write to the fileoff
- the start offset in the datalen
- the number of bytes to writeIOException
- in case of an I/O errorpublic static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException
len
bytes from the specified byte array starting
at offset off
to a file, creating the file if it does
not exist.file
- the file to write todata
- the content to write to the fileoff
- the start offset in the datalen
- the number of bytes to writeappend
- if true
, then bytes will be added to the
end of the file rather than overwritingIOException
- 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.
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 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, boolean append) throws IOException
toString()
value of each item in a collection to
the specified File
line by line, optionally appending.
The specified character encoding and the default line ending will be used.file
- the file to write toencoding
- the encoding to use, null
means platform defaultlines
- the lines to write, null
entries produce blank linesappend
- if true
, then the lines will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorUnsupportedEncodingException
- if the encoding is not supported by the VMpublic static void writeLines(File file, Collection<?> lines) throws IOException
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 linesIOException
- in case of an I/O errorpublic static void writeLines(File file, Collection<?> lines, boolean append) throws IOException
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 linesappend
- if true
, then the lines will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorpublic 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.
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 defaultIOException
- 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, boolean append) 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.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 defaultappend
- if true
, then the lines will be added to the
end of the file rather than overwritingIOException
- in case of an I/O errorUnsupportedEncodingException
- if the encoding is not supported by the VMpublic static void writeLines(File file, Collection<?> lines, String lineEnding) throws IOException
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 defaultIOException
- in case of an I/O errorpublic static void writeLines(File file, Collection<?> lines, String lineEnding, boolean append) throws IOException
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 defaultappend
- if true
, then the lines will be added to the
end of the file rather than overwritingIOException
- 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 void forceMkdirParent(File file) throws IOException
file
- file with parent to create, must not be null
NullPointerException
- if the file is null
IOException
- if the parent directory cannot be createdpublic 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.
Note that overflow is not detected, and the return value may be negative if
overflow occurs. See sizeOfAsBigInteger(File)
for an alternative
method that does not overflow.
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 BigInteger sizeOfAsBigInteger(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)
Note that overflow is not detected, and the return value may be negative if
overflow occurs. See sizeOfDirectoryAsBigInteger(File)
for an alternative
method that does not overflow.
directory
- directory to inspect, must not be null
Long.MAX_VALUE
.NullPointerException
- if the directory is null
public static BigInteger sizeOfDirectoryAsBigInteger(File directory)
directory
- directory to inspect, must not be null
NullPointerException
- if the directory is null
public 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, must not be null
reference
- the File
of which the modification date
is used, must not be null
File
exists and has been modified more
recently than the reference File
IllegalArgumentException
- if the file is null
IllegalArgumentException
- 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, must not be null
date
- the date reference, must not be null
File
exists and has been modified
after the given Date
.IllegalArgumentException
- if the file is null
IllegalArgumentException
- if the date is null
public 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, must not be null
timeMillis
- 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 null
public static boolean isFileOlder(File file, File reference)
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
File
exists and has been modified before
the reference File
IllegalArgumentException
- if the file is null
IllegalArgumentException
- if the reference file is null
or doesn't existpublic static boolean isFileOlder(File file, Date date)
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
File
exists and has been modified
before the given Date
.IllegalArgumentException
- if the file is null
IllegalArgumentException
- if the date is null
public static boolean isFileOlder(File file, long timeMillis)
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)File
exists and has been modified before
the given time reference.IllegalArgumentException
- if the file is null
public static long checksumCRC32(File file) throws IOException
file
- the file to checksum, must not be null
NullPointerException
- if the file or checksum is null
IllegalArgumentException
- if the file is a directoryIOException
- if an IO error occurs reading the filepublic static Checksum checksum(File file, Checksum checksum) throws IOException
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
NullPointerException
- if the file or checksum is null
IllegalArgumentException
- if the file is a directoryIOException
- if an IO error occurs reading the filepublic 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 moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) throws IOException
src
- the file to be moveddestDir
- the destination filecreateDestDir
- If true
create the destination directory,
otherwise if false
throw an IOExceptionNullPointerException
- if source or destination is null
FileExistsException
- if the directory exists in the destination directoryIOException
- 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 void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) throws IOException
srcFile
- the file to be moveddestDir
- the destination filecreateDestDir
- If true
create the destination directory,
otherwise if false
throw an IOExceptionNullPointerException
- 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 void moveToDirectory(File src, File destDir, boolean createDestDir) throws IOException
When the destination is on another file system, do a "copy and delete".
src
- the file or directory to be moveddestDir
- the destination directorycreateDestDir
- If true
create the destination directory,
otherwise if false
throw an IOExceptionNullPointerException
- if source or destination is null
FileExistsException
- if the directory or file exists in the destination directoryIOException
- 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.
When using jdk1.7, this method delegates to boolean java.nio.file.Files.isSymbolicLink(Path path)
Note: the current implementation always returns false
if running on
jkd1.6 and the system is detected as Windows using FilenameUtils.isSystemWindows()
For code that runs on Java 1.7 or later, use the following method instead:
boolean java.nio.file.Files.isSymbolicLink(Path path)
file
- the file to checkIOException
- if an IO error occurs while checking the fileCopyright © 2002–2016 The Apache Software Foundation. All rights reserved.