Class PathUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcleanDirectory(Path directory) Cleans a directory without deleting it.static voidcopyDirectory(Path srcDir, Path destDir) Copies a whole directory to a new location preserving the file dates.static voidcopyDirectory(Path srcDir, Path destDir, boolean preserveFileDate) Copies a whole directory to a new location.static voidcopyDirectory(Path srcDir, Path destDir, Predicate<Path> filter) Copies a filtered directory to a new location preserving the file dates.static voidcopyDirectory(Path srcDir, Path destDir, Predicate<Path> filter, boolean preserveFileDate) Copies a filtered directory to a new location.static voidCopies a file to a new location preserving the file date.static voidCopies a file to a new location.static voiddeleteDirectory(Path directory) Deletes a directory recursively.static booleandeleteQuietly(Path file) Deletes a file, never throwing an exception.static voidforceDelete(Path file) Deletes a file.static booleanDetermines whether the specified file is a Symbolic Link rather than an actual file.static voidmoveDirectory(Path srcDir, Path destDir) Moves a directory.static voidmoveDirectoryToDirectory(Path src, Path destDir, boolean createDestDir) Moves a directory to another directory.static voidMoves a file.static voidmoveFileToDirectory(Path srcFile, Path destDir, boolean createDestDir) Moves a file to a directory.static voidmoveToDirectory(Path src, Path destDir, boolean createDestDir) Moves a file or directory to the destination directory.
-
Method Details
-
copyDirectory
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 benulldestDir- the new directory, must not benull- Throws:
NullPointerException- if source or destination isnullIOException- if source or destination is invalidIOException- if an IO error occurs during copying
-
copyDirectory
public static void copyDirectory(Path srcDir, Path destDir, boolean preserveFileDate) throws IOException Copies a whole directory to a new location.This method copies the contents of the specified source directory to within the specified destination directory.
The destination directory is created if it does not exist. If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.
Note: Setting
preserveFileDatetotruetries 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 benulldestDir- the new directory, must not benullpreserveFileDate- true if the file date of the copy should be the same as the original- Throws:
NullPointerException- if source or destination isnullIOException- if source or destination is invalidIOException- if an IO error occurs during copying
-
copyDirectory
public static void copyDirectory(Path srcDir, Path destDir, Predicate<Path> filter) throws IOException Copies a filtered directory to a new location preserving the file dates.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.Example: Copy directories only
// only copy the directory structure FileUtils.copyDirectory(srcDir, destDir, Files::isDirectory);
- Parameters:
srcDir- an existing directory to copy, must not benulldestDir- the new directory, must not benullfilter- the filter to apply, null means copy all directories and files should be the same as the original- Throws:
NullPointerException- if source or destination isnullIOException- if source or destination is invalidIOException- if an IO error occurs during copying
-
copyDirectory
public static void copyDirectory(Path srcDir, Path destDir, Predicate<Path> filter, boolean preserveFileDate) throws 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
preserveFileDatetotruetries 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 benulldestDir- the new directory, must not benullfilter- 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:
NullPointerException- if source or destination isnullIOException- if source or destination is invalidIOException- if an IO error occurs during copying
-
moveFile
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:
NullPointerException- if source or destination isnullFileExistsException- if the destination file existsIOException- if source or destination is invalidIOException- if an IO error occurs moving the file- Since:
- 1.4
-
moveToDirectory
public static void moveToDirectory(Path src, Path destDir, boolean createDestDir) throws IOException Moves a file or directory to the destination directory.When the destination is on another file system, do a "copy and delete".
- Parameters:
src- the file or directory to be moveddestDir- the destination directorycreateDestDir- Iftruecreate the destination directory, otherwise iffalsethrow an IOException- Throws:
NullPointerException- if source or destination isnullFileExistsException- if the directory or file exists in the destination directoryIOException- if source or destination is invalidIOException- if an IO error occurs moving the file
-
moveFileToDirectory
public static void moveFileToDirectory(Path srcFile, Path destDir, boolean createDestDir) throws IOException Moves a file to a directory.- Parameters:
srcFile- the file to be moveddestDir- the destination filecreateDestDir- Iftruecreate the destination directory, otherwise iffalsethrow an IOException- Throws:
NullPointerException- if source or destination isnullFileExistsException- if the destination file existsIOException- if source or destination is invalidIOException- if an IO error occurs moving the file- Since:
- 1.4
-
moveDirectoryToDirectory
public static void moveDirectoryToDirectory(Path src, Path destDir, boolean createDestDir) throws IOException Moves a directory to another directory.- Parameters:
src- the file to be moveddestDir- the destination filecreateDestDir- Iftruecreate the destination directory, otherwise iffalsethrow an IOException- Throws:
NullPointerException- if source or destination isnullFileExistsException- if the directory exists in the destination directoryIOException- if source or destination is invalidIOException- if an IO error occurs moving the file
-
moveDirectory
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:
NullPointerException- if source or destination isnullFileExistsException- if the destination directory existsIOException- if source or destination is invalidIOException- if an IO error occurs moving the file
-
deleteQuietly
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:
trueif the file or directory was deleted, otherwisefalse
-
cleanDirectory
Cleans a directory without deleting it.- Parameters:
directory- directory to clean- Throws:
IOException- in case cleaning is unsuccessfulIllegalArgumentException- ifdirectorydoes not exist or is not a directory
-
forceDelete
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:
NullPointerException- if the directory isnullFileNotFoundException- if the file was not foundIOException- in case deletion is unsuccessful
-
deleteDirectory
Deletes a directory recursively.- Parameters:
directory- directory to delete- Throws:
IOException- in case deletion is unsuccessfulIllegalArgumentException- ifdirectorydoes not exist or is not a directory
-
isSymlink
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.
When using jdk1.7, this method delegates to
boolean java.nio.file.Files.isSymbolicLink(Path path)For code that runs on Java 1.7 or later, use the following method instead:
boolean java.nio.file.Files.isSymbolicLink(Path path)- Parameters:
file- the file to check- Returns:
- true if the file is a Symbolic Link
- Throws:
IOException- if an IO error occurs while checking the file
-
copyFile
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 benulldestFile- the new file, must not benull- Throws:
NullPointerException- if source or destination isnullIOException- 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 completes- See Also:
-
copyFile
public static void copyFile(Path srcFile, Path destFile, boolean preserveFileDate) throws IOException Copies a file to a new location.This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
Note: Setting
preserveFileDatetotruetries 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 benulldestFile- the new file, must not benullpreserveFileDate- true if the file date of the copy should be the same as the original- Throws:
NullPointerException- if source or destination isnullIOException- 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 completes- See Also:
-
doCopyFile(Path, Path, boolean)
-