Class PathUtils
- java.lang.Object
-
- org.ametys.core.util.path.PathUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
cleanDirectory(Path directory)
Cleans a directory without deleting it.static void
copyDirectory(Path srcDir, Path destDir)
Copies a whole directory to a new location preserving the file dates.static void
copyDirectory(Path srcDir, Path destDir, boolean preserveFileDate)
Copies a whole directory to a new location.static void
copyDirectory(Path srcDir, Path destDir, Predicate<Path> filter)
Copies a filtered directory to a new location preserving the file dates.static void
copyDirectory(Path srcDir, Path destDir, Predicate<Path> filter, boolean preserveFileDate)
Copies a filtered directory to a new location.static void
copyFile(Path srcFile, Path destFile)
Copies a file to a new location preserving the file date.static void
copyFile(Path srcFile, Path destFile, boolean preserveFileDate)
Copies a file to a new location.static void
deleteDirectory(Path directory)
Deletes a directory recursively.static boolean
deleteQuietly(Path file)
Deletes a file, never throwing an exception.static void
forceDelete(Path file)
Deletes a file.static boolean
isSymlink(Path file)
Determines whether the specified file is a Symbolic Link rather than an actual file.static void
moveDirectory(Path srcDir, Path destDir)
Moves a directory.static void
moveDirectoryToDirectory(Path src, Path destDir, boolean createDestDir)
Moves a directory to another directory.static void
moveFile(Path srcFile, Path destFile)
Moves a file.static void
moveFileToDirectory(Path srcFile, Path destDir, boolean createDestDir)
Moves a file to a directory.static void
moveToDirectory(Path src, Path destDir, boolean createDestDir)
Moves a file or directory to the destination directory.
-
-
-
Method Detail
-
copyDirectory
public static void copyDirectory(Path srcDir, Path destDir) throws 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:
NullPointerException
- if source or destination isnull
IOException
- 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
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:
NullPointerException
- if source or destination isnull
IOException
- 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 benull
destDir
- the new directory, must not benull
filter
- the filter to apply, null means copy all directories and files should be the same as the original- Throws:
NullPointerException
- if source or destination isnull
IOException
- 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
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:
NullPointerException
- if source or destination isnull
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copying
-
moveFile
public static void moveFile(Path srcFile, Path destFile) throws 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:
NullPointerException
- if source or destination isnull
FileExistsException
- 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
- Iftrue
create the destination directory, otherwise iffalse
throw an IOException- Throws:
NullPointerException
- if source or destination isnull
FileExistsException
- 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
- Iftrue
create the destination directory, otherwise iffalse
throw an IOException- Throws:
NullPointerException
- if source or destination isnull
FileExistsException
- 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
- Iftrue
create the destination directory, otherwise iffalse
throw an IOException- Throws:
NullPointerException
- if source or destination isnull
FileExistsException
- if the directory exists in the destination directoryIOException
- if source or destination is invalidIOException
- if an IO error occurs moving the file
-
moveDirectory
public static void moveDirectory(Path srcDir, Path destDir) throws 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:
NullPointerException
- if source or destination isnull
FileExistsException
- if the destination directory existsIOException
- if source or destination is invalidIOException
- if an IO error occurs moving the file
-
deleteQuietly
public static boolean deleteQuietly(Path 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
-
cleanDirectory
public static void cleanDirectory(Path directory) throws IOException
Cleans a directory without deleting it.- Parameters:
directory
- directory to clean- Throws:
IOException
- in case cleaning is unsuccessfulIllegalArgumentException
- ifdirectory
does not exist or is not a directory
-
forceDelete
public static void forceDelete(Path file) throws 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:
NullPointerException
- if the directory isnull
FileNotFoundException
- if the file was not foundIOException
- in case deletion is unsuccessful
-
deleteDirectory
public static void deleteDirectory(Path directory) throws IOException
Deletes a directory recursively.- Parameters:
directory
- directory to delete- Throws:
IOException
- in case deletion is unsuccessfulIllegalArgumentException
- ifdirectory
does not exist or is not a directory
-
isSymlink
public static boolean isSymlink(Path file) throws 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.
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
public static void copyFile(Path srcFile, Path destFile) throws 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:
NullPointerException
- if source or destination isnull
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 completes- See Also:
copyFile(Path, Path, boolean)
-
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
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:
NullPointerException
- if source or destination isnull
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 completes- See Also:
doCopyFile(Path, Path, boolean)
-
-