Files.-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcleanRecursively(Path path) Attempt to recursively clean the file or directory at the given path.static voidAttempt to recursively clean all of the files returned by the given directory stream.static voidcleanRecursively(SecureDirectoryStream<Path> sds, Path path) Attempt to recursively clean the file or directory at the given directory and path.static voidAttempt to recursively clean all of the files and directories from the given stream, even if secure directory streams are not supported.static voidAttempt to recursively clean the file or directory at the given path, even if secure directory streams are not supported.static longcopy(ReadableByteChannel in, WritableByteChannel out) Copy from one channel to another in the most efficient manner supported by the JDK.static voidEquivalent to callingcopy(Path, Path, CopyOption...)with no options.static voidcopy(Path srcFile, Path destFile, CopyOption option1) Equivalent to callingcopy(Path, Path, CopyOption...)with one option.static voidcopy(Path srcFile, Path destFile, CopyOption... options) Copy a file as securely as possible from one path to another.static voidcopy(Path srcFile, Path destFile, CopyOption option1, CopyOption option2) Equivalent to callingcopy(Path, Path, CopyOption...)with two options.static voidcopy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile) Equivalent to callingcopy(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with no options.static voidcopy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1) Equivalent to callingcopy(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with one option.static voidcopy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption... options) Copy a file as securely as possible from one directory and path to another.static voidcopy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1, CopyOption option2) Equivalent to callingcopy(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with two options.static voidcopyEvenIfInsecure(Path srcFile, Path destFile) Copy a file, even if secure directory streams are not supported.static voidcopyRecursively(Path srcFile, Path destFile) Equivalent to callingcopyRecursively(Path, Path, CopyOption...)with no options.static voidcopyRecursively(Path srcFile, Path destFile, CopyOption option1) Equivalent to callingcopyRecursively(Path, Path, CopyOption...)with one option.static voidcopyRecursively(Path srcFile, Path destFile, CopyOption... options) Recursively copy a file or directory as securely as possible from one path to another.static voidcopyRecursively(Path srcFile, Path destFile, CopyOption option1, CopyOption option2) Equivalent to callingcopyRecursively(Path, Path, CopyOption...)with two options.static voidcopyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile) Equivalent to callingcopyRecursively(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with no options.static voidcopyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1) Equivalent to callingcopyRecursively(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with one option.static voidcopyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption... options) Recursively copy a file or directory as securely as possible from one directory and path to another.static voidcopyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1, CopyOption option2) Equivalent to callingcopyRecursively(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with two options.static voidcopyRecursivelyEvenIfInsecure(Path srcFile, Path destFile) Recursively copy a file or directory, even if secure directory streams are not supported.static PathReturns the current working directory path at the time that this program was started (notnull).static UserPrincipalReturns theUserPrincipalcorresponding to the current process user (notnull).static voiddeleteRecursively(Path path) Attempt to recursively delete the file or directory at the given path.static voidAttempt to recursively delete all of the files returned by the given directory stream.static voiddeleteRecursively(SecureDirectoryStream<Path> sds, Path path) Attempt to recursively delete the file or directory at the given directory and path.static voidAttempt to recursively delete all of the files returned by the given directory stream, even if secure directory streams are not supported.static voidAttempt to recursively delete the file or directory at the given path, even if secure directory streams are not supported.static DeleteStatsdeleteRecursivelyQuietly(Path path) Attempt to recursively delete the file or directory at the given path.static DeleteStatsAttempt to recursively delete all of the files returned by the given directory stream.static DeleteStatsdeleteRecursivelyQuietly(SecureDirectoryStream<Path> sds, Path path) Attempt to recursively delete the file or directory at the given directory and path.static DeleteStatsAttempt to recursively delete all of the files returned by the given directory stream, even if secure directory streams are not supported.static DeleteStatsAttempt to recursively delete the file or directory at the given path, even if secure directory streams are not supported.static PathReturns the parent of the given path, even if it is relative, ornullif the path has no parent.static booleanReturnstrueif this platform has secure directories, orfalseif it does not.static voidEquivalent to callingmove(Path, Path, CopyOption...)with no options.static voidmove(Path srcFile, Path destFile, CopyOption option1) Equivalent to callingmove(Path, Path, CopyOption...)with one option.static voidmove(Path srcFile, Path destFile, CopyOption... options) Move a file as securely as possible from one path to another.static voidmove(Path srcFile, Path destFile, CopyOption option1, CopyOption option2) Equivalent to callingmove(Path, Path, CopyOption...)with two options.static voidmove(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile) Equivalent to callingmove(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with no options.static voidmove(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1) Equivalent to callingmove(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with one option.static voidmove(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption... options) Move a file as securely as possible from one directory and path to another.static voidmove(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1, CopyOption option2) Equivalent to callingmove(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with two options.static voidmoveEvenIfInsecure(Path srcFile, Path destFile) Move a file or directory, even if secure directory streams are not supported.static SecureDirectoryStream<Path> newSecureDirectoryStream(Path path, LinkOption... linkOptions) Open a new secure directory stream at the given path.static BufferedFileopenBuffered(Path path, OpenOption... options) Open a buffered random-access file at the given path.static BufferedFileopenBuffered(Path path, Collection<? extends OpenOption> options) Open a buffered random-access file at the given path.static BufferedFileopenBuffered(Path path, Collection<? extends OpenOption> options, Collection<? extends FileAttribute<?>> attrs) Open a buffered random-access file at the given path, atomically setting the given file attributes if the file is created.static FileAttribute<?> Returns the directory attribute that can be used to make a directory private to the current user (notnull).static FileAttribute<?> Returns the file attribute that can be used to make a file private to the current user (notnull).
-
Method Details
-
newSecureDirectoryStream
public static SecureDirectoryStream<Path> newSecureDirectoryStream(Path path, LinkOption... linkOptions) throws IOException Open a new secure directory stream at the given path. If the link optionLinkOption.NOFOLLOW_LINKSis given, the open will fail with an exception ifpathis a symbolic link.- Parameters:
path- the path of the directory to open (must not benull)linkOptions- the link options (must not benull)- Returns:
- a secure directory stream for the given path (not
null) - Throws:
NotDirectoryException- if the targetpathis not a directoryIOException- if another I/O error occursUnsupportedOperationException- if this platform does not support secure directory streams- See Also:
-
deleteRecursivelyEvenIfInsecure
Attempt to recursively delete the file or directory at the given path, even if secure directory streams are not supported. If the target path is a symbolic link, it will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.- Parameters:
path- the path to delete (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occurs- See Also:
-
deleteRecursivelyEvenIfInsecure
Attempt to recursively delete all of the files returned by the given directory stream, even if secure directory streams are not supported. If any of the target paths are symbolic links, they will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.The directory stream is not closed by this operation. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
ds- the directory stream whose contents should be removed (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occurs- See Also:
-
deleteRecursively
Attempt to recursively delete the file or directory at the given path. If the target path is a symbolic link, it will be removed.- Parameters:
path- the file or directory to be removed (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occursUnsupportedOperationException- if secure directory removal is unsupported
-
deleteRecursively
Attempt to recursively delete all of the files returned by the given directory stream. If any of the target paths are symbolic links, they will be removed.The directory stream is not closed by this operation, but its iterator is consumed. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
sds- the directory stream whose contents should be removed (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occurs
-
deleteRecursively
Attempt to recursively delete the file or directory at the given directory and path. If the target path or any nested path is a symbolic link, it will be removed and will not be treated as a directory. If the target path is absolute, thensdsis ignored and the absolute path is removed. The caller should ensure that the given path is sanitized if needed (seePath.normalize()andPath.isAbsolute()).The directory stream is not closed by this operation, and its iterator is not consumed. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
sds- the directory stream containing the file (must not benull)path- the relative path of the file or directory to be removed (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occurs
-
deleteRecursivelyQuietlyEvenIfInsecure
Attempt to recursively delete the file or directory at the given path, even if secure directory streams are not supported. I/O errors will not be reported. If the target path is a symbolic link, it will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.- Parameters:
path- the path to delete (must not benull)- Returns:
- the deletion statistics (not
null) - See Also:
-
deleteRecursivelyQuietlyEvenIfInsecure
Attempt to recursively delete all of the files returned by the given directory stream, even if secure directory streams are not supported. I/O errors will not be reported. If any of the target paths are symbolic links, they will be removed. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.The directory stream is not closed by this operation. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
ds- the directory stream whose contents should be removed (must not benull)- Returns:
- the deletion statistics (not
null) - See Also:
-
deleteRecursivelyQuietly
Attempt to recursively delete the file or directory at the given path. If the target path is a symbolic link, it will be removed. I/O errors will not be reported.- Parameters:
path- the file or directory to be removed (must not benull)- Returns:
- the deletion statistics (not
null) - Throws:
UnsupportedOperationException- if secure directory removal is unsupported
-
deleteRecursivelyQuietly
Attempt to recursively delete all of the files returned by the given directory stream. If any of the target paths are symbolic links, they will be removed. I/O errors will not be reported.The directory stream is not closed by this operation, but its iterator is consumed. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
sds- the directory stream whose contents should be removed (must not benull)- Returns:
- the deletion statistics (not
null)
-
deleteRecursivelyQuietly
Attempt to recursively delete the file or directory at the given directory and path. If the target path or any nested path is a symbolic link, it will be removed and will not be treated as a directory. If the target path is absolute, thensdsis ignored and the absolute path is removed. I/O errors will not be reported. The caller should ensure that the given path is sanitized if needed (seePath.normalize()andPath.isAbsolute()).The directory stream is not closed by this operation, and its iterator is not consumed. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
sds- the directory stream containing the file (must not benull)path- the relative path of the file or directory to be removed (must not benull)- Returns:
- the deletion statistics (not
null)
-
cleanRecursivelyEvenIfInsecure
Attempt to recursively clean the file or directory at the given path, even if secure directory streams are not supported. If the target path or any nested path is not a directory, it will be removed. Directories will be preserved. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.- Parameters:
path- the path to clean (must not benull)- Throws:
IOException- if one or more files fails to be cleaned or another I/O error occurs- See Also:
-
cleanRecursivelyEvenIfInsecure
Attempt to recursively clean all of the files and directories from the given stream, even if secure directory streams are not supported. If any nested path is not a directory, it will be removed. Directories will be preserved. Warning: this method can potentially delete files outside the intended path if the target platform does not support secure directory iteration.The directory stream is not closed by this operation. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
ds- the directory stream whose contents should be cleaned (must not benull)- Throws:
IOException- if one or more files fails to be cleaned or another I/O error occurs- See Also:
-
cleanRecursively
Attempt to recursively clean the file or directory at the given path. If the target path is not a directory, it will be removed.- Parameters:
path- the file or directory to be cleaned (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occursUnsupportedOperationException- if secure directory traversal is unsupported
-
cleanRecursively
Attempt to recursively clean all of the files returned by the given directory stream. If any of the target paths are not directories, they will be removed.The directory stream is not closed by this operation, but its iterator is consumed. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
sds- the directory stream whose contents should be cleaned (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occurs
-
cleanRecursively
Attempt to recursively clean the file or directory at the given directory and path. If the target path or any nested path is a symbolic link, it will be removed and will not be treated as a directory. If any of the target paths are not directories, they will be removed. If the target path is absolute, thensdsis ignored and the absolute path is cleaned. The caller should ensure that the given path is sanitized if needed (seePath.normalize()andPath.isAbsolute()).The directory stream is not closed by this operation, and its iterator is not consumed. The caller should ensure that the stream is closed at the appropriate time.
- Parameters:
sds- the directory stream containing the file (must not benull)path- the relative path of the file or directory to be cleaned (must not benull)- Throws:
IOException- if one or more files fails to be deleted or another I/O error occurs
-
getParent
Returns the parent of the given path, even if it is relative, ornullif the path has no parent. Relative paths are resolved relative to the current directory.- Parameters:
path- the path to examine (must not benull)- Returns:
- the parent of the given path, even if it is relative, or
nullif the path has no parent - See Also:
-
copy
Copy from one channel to another in the most efficient manner supported by the JDK. Neither channel is closed.- Parameters:
in- the input channel (must not benull)out- the output channel (must not benull)- Returns:
- the number of bytes copied
- Throws:
IOException- if copying failed for some reason
-
copy
public static void copy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile) throws IOException Equivalent to callingcopy(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with no options.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copy
public static void copy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1) throws IOException Equivalent to callingcopy(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with one option.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)option1- the copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copy
public static void copy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1, CopyOption option2) throws IOException Equivalent to callingcopy(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with two options.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)option1- the first copy option to use for the operation (must not benull)option2- the second copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copy
public static void copy(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption... options) throws IOException Copy a file as securely as possible from one directory and path to another. If bothsrcFileanddestFileare absolute paths, then this method behaves exactly the same asFiles.copy(Path, Path, CopyOption...). IfsrcFileanddestFileare found to refer to the same file, then no action is taken and this method will return directly.Otherwise, an attempt is made to:
- Copy symbolic links as symbolic links (when
LinkOption.NOFOLLOW_LINKSis given) - Copy directories as directories (non-recursively)
- Preserve file attributes (when
StandardCopyOption.COPY_ATTRIBUTESis given) - Copy regular files using the most efficient JDK mechanism available
IOExceptionif some part of the operation fails; in this case, the file may be partially copied.If some aspect of the operation is not supported,
UnsupportedOperationExceptionwill be thrown. Currently, the currently unsupported operations include:- Copying a symbolic link from or to a relative path (unsupported by the JDK as of JDK 25)
- Copying a directory into a relative path (unsupported by the JDK as of JDK 25)
IllegalArgumentExceptionto be thrown.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)options- the copy options to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
- Copy symbolic links as symbolic links (when
-
copyEvenIfInsecure
Copy a file, even if secure directory streams are not supported. Warning: this method can potentially copy files outside the intended path if the target platform does not support secure directory iteration.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)- Throws:
IOException- if an I/O error occurs during copy- See Also:
-
copy
Equivalent to callingcopy(Path, Path, CopyOption...)with no options.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copy
Equivalent to callingcopy(Path, Path, CopyOption...)with one option.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)option1- the copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copy
public static void copy(Path srcFile, Path destFile, CopyOption option1, CopyOption option2) throws IOException Equivalent to callingcopy(Path, Path, CopyOption...)with two options.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)option1- the first copy option to use for the operation (must not benull)option2- the second copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copy
Copy a file as securely as possible from one path to another. This method uses a secure directory stream acquired from the current working directory to perform the operation. IfsrcFileanddestFileare found to refer to the same file, then no action is taken and this method will return directly.Otherwise, an attempt is made to:
- Copy symbolic links as symbolic links (when
LinkOption.NOFOLLOW_LINKSis given) - Copy directories as directories (non-recursively)
- Preserve file attributes (when
StandardCopyOption.COPY_ATTRIBUTESis given) - Copy regular files using the most efficient JDK mechanism available
IOExceptionif some part of the operation fails; in this case, the file may be partially copied.If some aspect of the operation is not supported,
UnsupportedOperationExceptionwill be thrown. Currently, the currently unsupported operations include:- Copying a symbolic link from or to a relative path (unsupported by the JDK as of JDK 25)
- Copying a directory into a relative path (unsupported by the JDK as of JDK 25)
IllegalArgumentExceptionto be thrown.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)options- the copy options to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
- Copy symbolic links as symbolic links (when
-
copyRecursively
public static void copyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile) throws IOException Equivalent to callingcopyRecursively(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with no options.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copyRecursively
public static void copyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1) throws IOException Equivalent to callingcopyRecursively(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with one option.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)option1- the copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copyRecursively
public static void copyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1, CopyOption option2) throws IOException Equivalent to callingcopyRecursively(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with two options.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)option1- the first copy option to use for the operation (must not benull)option2- the second copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copyRecursively
public static void copyRecursively(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption... options) throws IOException Recursively copy a file or directory as securely as possible from one directory and path to another. IfsrcFileanddestFileare found to refer to the same file, then no action is taken and this method will return directly.Otherwise, an attempt is made to:
- Copy symbolic links as symbolic links (when
LinkOption.NOFOLLOW_LINKSis given) - Copy directories as directories (recursively)
- Preserve file attributes (when
StandardCopyOption.COPY_ATTRIBUTESis given) - Copy regular files using the most efficient JDK mechanism available
IOExceptionif some part of the operation fails; in this case, the file or directory may be partially copied.If some aspect of the operation is not supported,
UnsupportedOperationExceptionwill be thrown. Currently, the currently unsupported operations include:- Copying a symbolic link from or to a relative path (unsupported by the JDK as of JDK 25)
- Copying a directory into a relative path (unsupported by the JDK as of JDK 25)
IllegalArgumentExceptionto be thrown.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)options- the copy options to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy
- Copy symbolic links as symbolic links (when
-
copyRecursivelyEvenIfInsecure
Recursively copy a file or directory, even if secure directory streams are not supported. Warning: this method can potentially copy files outside the intended path if the target platform does not support secure directory iteration.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)- Throws:
IOException- if an I/O error occurs during copy- See Also:
-
copyRecursively
Equivalent to callingcopyRecursively(Path, Path, CopyOption...)with no options.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copyRecursively
public static void copyRecursively(Path srcFile, Path destFile, CopyOption option1) throws IOException Equivalent to callingcopyRecursively(Path, Path, CopyOption...)with one option.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)option1- the copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copyRecursively
public static void copyRecursively(Path srcFile, Path destFile, CopyOption option1, CopyOption option2) throws IOException Equivalent to callingcopyRecursively(Path, Path, CopyOption...)with two options.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)option1- the first copy option to use for the operation (must not benull)option2- the second copy option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
-
copyRecursively
public static void copyRecursively(Path srcFile, Path destFile, CopyOption... options) throws IOException Recursively copy a file or directory as securely as possible from one path to another. This method uses a secure directory stream acquired from the current working directory to perform the operation. IfsrcFileanddestFileare found to refer to the same file, then no action is taken and this method will return directly.Otherwise, an attempt is made to:
- Copy symbolic links as symbolic links (when
LinkOption.NOFOLLOW_LINKSis given) - Copy directories as directories (recursively)
- Preserve file attributes (when
StandardCopyOption.COPY_ATTRIBUTESis given) - Copy regular files using the most efficient JDK mechanism available
IOExceptionif some part of the operation fails; in this case, the file or directory may be partially copied.If some aspect of the operation is not supported,
UnsupportedOperationExceptionwill be thrown. Currently, the currently unsupported operations include:- Copying a symbolic link from or to a relative path (unsupported by the JDK as of JDK 25)
- Copying a directory into a relative path (unsupported by the JDK as of JDK 25)
IllegalArgumentExceptionto be thrown.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)options- the copy options to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during copy- See Also:
- Copy symbolic links as symbolic links (when
-
move
public static void move(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile) throws IOException Equivalent to callingmove(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with no options.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
-
move
public static void move(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1) throws IOException Equivalent to callingmove(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with one option.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)option1- the move option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
-
move
public static void move(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption option1, CopyOption option2) throws IOException Equivalent to callingmove(SecureDirectoryStream, Path, SecureDirectoryStream, Path, CopyOption...)with two options.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)option1- the first move option to use for the operation (must not benull)option2- the second move option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
-
move
public static void move(SecureDirectoryStream<Path> sourceDir, Path srcFile, SecureDirectoryStream<Path> destDir, Path destFile, CopyOption... options) throws IOException Move a file as securely as possible from one directory and path to another. If bothsrcFileanddestFileare absolute paths, then this method behaves exactly the same asFiles.move(Path, Path, CopyOption...). IfsrcFileanddestFileare found to refer to the same file, then no action is taken and this method will return directly.Otherwise, an attempt is made to:
- Move symbolic links as symbolic links (when
LinkOption.NOFOLLOW_LINKSis given) - Move directories as directories (including their contents)
- Preserve file attributes (when
StandardCopyOption.COPY_ATTRIBUTESis given) - Move regular files using the most efficient JDK mechanism available
IOExceptionif some part of the operation fails; in this case, the file may be partially copied.If some aspect of the operation is not supported,
UnsupportedOperationExceptionwill be thrown. Currently, the currently unsupported operations include:- Copying a symbolic link from or to a relative path (unsupported by the JDK as of JDK 25)
- Copying a directory into a relative path (unsupported by the JDK as of JDK 25)
LinkOption.NOFOLLOW_LINKSStandardCopyOption.COPY_ATTRIBUTESStandardCopyOption.REPLACE_EXISTINGStandardCopyOption.ATOMIC_MOVE
IllegalArgumentExceptionto be thrown.- Parameters:
sourceDir- the directory stream of the source directory (must not benull)srcFile- the source path name (must not benull)destDir- the directory stream of the destination directory (must not benull)destFile- the destination path name (must not benull)options- the move options to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
- Move symbolic links as symbolic links (when
-
moveEvenIfInsecure
Move a file or directory, even if secure directory streams are not supported. Warning: this method can potentially move files outside the intended path if the target platform does not support secure directory iteration.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)- Throws:
IOException- if an I/O error occurs during move- See Also:
-
move
Equivalent to callingmove(Path, Path, CopyOption...)with no options.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
-
move
Equivalent to callingmove(Path, Path, CopyOption...)with one option.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)option1- the move option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
-
move
public static void move(Path srcFile, Path destFile, CopyOption option1, CopyOption option2) throws IOException Equivalent to callingmove(Path, Path, CopyOption...)with two options.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)option1- the first move option to use for the operation (must not benull)option2- the second move option to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
-
move
Move a file as securely as possible from one path to another. This method uses a secure directory stream acquired from the current working directory to perform the operation. IfsrcFileanddestFileare found to refer to the same file, then no action is taken and this method will return directly.Otherwise, an attempt is made to:
- Move symbolic links as symbolic links (when
LinkOption.NOFOLLOW_LINKSis given) - Move directories as directories (including their contents)
- Preserve file attributes (when
StandardCopyOption.COPY_ATTRIBUTESis given) - Move regular files using the most efficient JDK mechanism available
IOExceptionif some part of the operation fails; in this case, the file may be partially copied.If some aspect of the operation is not supported,
UnsupportedOperationExceptionwill be thrown. Currently, the currently unsupported operations include:- Copying a symbolic link from or to a relative path (unsupported by the JDK as of JDK 25)
- Copying a directory into a relative path (unsupported by the JDK as of JDK 25)
LinkOption.NOFOLLOW_LINKSStandardCopyOption.COPY_ATTRIBUTESStandardCopyOption.REPLACE_EXISTINGStandardCopyOption.ATOMIC_MOVE
IllegalArgumentExceptionto be thrown.- Parameters:
srcFile- the source path name (must not benull)destFile- the destination path name (must not benull)options- the move options to use for the operation (must not benull)- Throws:
UnsupportedOperationException- if secure directory traversal is unsupported or if the combination of options is unsupported by the source or destination filesystem or the JDK itselfIOException- if an I/O error occurs during move- See Also:
- Move symbolic links as symbolic links (when
-
currentDirectory
Returns the current working directory path at the time that this program was started (notnull). This path comes from theuser.dirsystem property.- Returns:
- the current working directory path at the time that this program was started (not
null)
-
hasSecureDirectories
public static boolean hasSecureDirectories()Returnstrueif this platform has secure directories, orfalseif it does not. Some operating systems or JVM versions do not support secure directories.- Returns:
trueif this platform has secure directories, orfalseif it does not
-
openBuffered
Open a buffered random-access file at the given path.The options may include most
StandardOpenOptionvalues, as well asBufferSizeOptionandByteOrderOptionto configure the buffer size and default byte order respectively. If noBufferSizeOptionis given, a default ofBufferSizeOption.DEFAULT_BUFFER_SIZEis used. If noByteOrderOptionis given, the native byte order is used. If neitherREADnorWRITEis specified, the file is opened for reading only.Note that
APPENDmode is not supported for random-access buffered files.- Parameters:
path- the path to the file (must not benull)options- the open options (must not benull, and must not containnullelements)- Returns:
- a new
BufferedFileinstance - Throws:
IOException- if an I/O error occursFileAlreadyExistsException- ifCREATE_NEWis specified and the file already existsIllegalArgumentException- if conflicting options are specified (e.g., bothByteOrderOption.BIG_ENDIANandByteOrderOption.LITTLE_ENDIAN)UnsupportedOperationException- if an unsupported option is specified
-
openBuffered
public static BufferedFile openBuffered(Path path, Collection<? extends OpenOption> options) throws IOException Open a buffered random-access file at the given path.The options may include most
StandardOpenOptionvalues, as well asBufferSizeOptionandByteOrderOptionto configure the buffer size and default byte order respectively. If noBufferSizeOptionis given, a default ofBufferSizeOption.DEFAULT_BUFFER_SIZEis used. If noByteOrderOptionis given, the native byte order is used. If neitherREADnorWRITEis specified, the file is opened for reading only.Note that
APPENDmode is not supported for random-access buffered files.- Parameters:
path- the path to the file (must not benull)options- the open options (must not benull, and must not containnullelements)- Returns:
- a new
BufferedFileinstance - Throws:
IOException- if an I/O error occursFileAlreadyExistsException- ifCREATE_NEWis specified and the file already existsIllegalArgumentException- if conflicting options are specified (e.g., bothByteOrderOption.BIG_ENDIANandByteOrderOption.LITTLE_ENDIAN)UnsupportedOperationException- if an unsupported option is specified
-
openBuffered
public static BufferedFile openBuffered(Path path, Collection<? extends OpenOption> options, Collection<? extends FileAttribute<?>> attrs) throws IOException Open a buffered random-access file at the given path, atomically setting the given file attributes if the file is created.The options may include most
StandardOpenOptionvalues, as well asBufferSizeOptionandByteOrderOptionto configure the buffer size and default byte order respectively. If noBufferSizeOptionis given, a default ofBufferSizeOption.DEFAULT_BUFFER_SIZEis used. If noByteOrderOptionis given, the native byte order is used. If neitherREADnorWRITEis specified, the file is opened for reading only.The
attrsparameter specifies file attributes to set atomically when creating the file. If the file already exists and theCREATEoption is given, the attributes are ignored. If the file already exists and theCREATE_NEWoption is given, aFileAlreadyExistsExceptionis thrown as usual.Note that
APPENDmode is not supported for random-access buffered files.- Parameters:
path- the path to the file (must not benull)options- the open options (must not benull, and must not containnullelements)attrs- the file attributes to set atomically when creating the file (must not benull)- Returns:
- a new
BufferedFileinstance - Throws:
IOException- if an I/O error occursFileAlreadyExistsException- ifCREATE_NEWis specified and the file already existsIllegalArgumentException- if conflicting options are specified (e.g., bothByteOrderOption.BIG_ENDIANandByteOrderOption.LITTLE_ENDIAN)UnsupportedOperationException- if an unsupported option is specified
-
currentUserPrincipal
Returns theUserPrincipalcorresponding to the current process user (notnull). The principal is looked up via the default filesystem'suser principal lookup service.- Returns:
- the
UserPrincipalcorresponding to the current process user (notnull) - Throws:
IOError- if the user principal lookup fails
-
privateFileAttribute
Returns the file attribute that can be used to make a file private to the current user (notnull). The attribute value depends on the operating system.- Returns:
- the file attribute that can be used to make a file private to the current user (not
null) - Throws:
IOError- if the user principal lookup fails
-
privateDirectoryAttribute
Returns the directory attribute that can be used to make a directory private to the current user (notnull). The attribute value depends on the operating system.- Returns:
- the directory attribute that can be used to make a directory private to the current user (not
null) - Throws:
IOError- if the user principal lookup fails
-