If you encounter a problem with File.renameto moving files across file systems, you should use the Files.move () interface or move files on the same file system.
File.renameto Interface Description:
public boolean Renameto (File dest)
Renames the file denoted by this abstract pathname.
Many aspects of the behavior of this method is inherently platform-dependent:the rename operation might not being able to m Ove a file from one filesystem to another, it might not being atomic, and it might not succeed if a file with the destination Abstract pathname already exists. The return value should always being checked to make sure, the rename operation was successful.
Note that the Files
class defines the move
method to move or rename a file in a platform independent manner.
- Parameters:
-
dest
-The new abstract pathname for the named file
- Returns:
-
true
If and only if the renaming succeeded;
false
otherwise
- Throws:
-
SecurityException
-If a security manager exists and its
SecurityManager.checkWrite(java.lang.String)
method denies write access to either the old or new pathnames
-
NullPointerException
-
dest
If
null
parameter
is
Files.move Interface Description:
-
public static path Move (path source, path target, copyoption ... options) throws IOException
Move or rename a file to a target file.
By default, this method attempts to move the file to the target file, failing if the target file exists except if the sour Ce and Target are thesame
file, in which case the This method has no effect. If The file is a symbolic link and the symbolic link itself, not the target of the link, is moved. This method is invoked to move an empty directory. In some implementations a directory have entries for special files or links that was created when the directory is created. In such implementations a directory are considered empty when only the special entries exist. When invoked to move a directory that's not empty and the directory is moved if it does not require moving the entries I n the directory. For example, renaming a directory on the sameFileStore
Won't usually require moving the entries in the directory. When moving a directory requires, its entries is moved then this method fails (by throwing anIOException
). To move aFile Treemay involve copying rather than moving directories and this can is done using thecopy
Method in conjunction with theFiles.walkFileTree
Utility method.
The parameter may include any of the options
following:
Option |
Description |
REPLACE_EXISTING |
If the target file exists, then the target file is replaced if it's not a non-empty directory. If the target file exists and is a symbolic link, then the symbolic link itself isn't the target of the link, is replaced. |
ATOMIC_MOVE |
The move is performed as a atomic file system operation and all other options are ignored. If the target file exists then it's implementation specific if the existing file is replaced or this method fails by thro Wing an IOException . If The move cannot be performed as a atomic file system operation then is AtomicMoveNotSupportedException thrown. This can arise, if example, when the target was on a different and FileStore would require, the file was copied, O R Target is associated with a different provider to this object. |
An implementation's interface may support additional implementation specific options.
Where The move requires that the file was copied then the was copied to the last-modified-time
new file. An implementation could also attempt to copy other file attributes and is not required to fail if the file attributes cannot be copied. When the move is performed as a non-atomic operation, and A are IOException
thrown, then the state of the files are not defined. T He original file and the target file may both exist, the target file is incomplete or some of its file attributes may Not been copied from the original file.
Usage Examples: Suppose we want to rename a file to ' newname ', keeping the file in the same directory:
Path Source = ...
Alternatively, suppose we want to move a file to new directory, keeping the same file name, and replacing any existing Fil E of that name in the directory:
Path Source = ... Path Newdir = ...
-
- Parameters:
-
-
source
-The path
to the file to move
-
-The path to the
-
target
target file (could be associated with a different provider to the source path)
-
-
options
-Options Specifying how the move should
is done
-
- Returns:
-
The path to the
-
target file
-
- Throws:
-
-
UnsupportedOperationException
-If the array contains a copy option
that's not supported
-
-
FileAlreadyExistsException
-If the target file exists but cannot was replaced because the
REPLACE_EXISTING
option is not specified
(optional s Pecific exception)
-
-the option is specified but the
-
DirectoryNotEmptyException
REPLACE_EXISTING
file cannot being replaced because it is a non-empty directory
(opt ional specific exception)
-
-
AtomicMoveNotSupportedException
-If the options array contains the
ATOMIC_MOVE
option but the file cannot is moved as an atomic file system Operat Ion.
-
-
IOException
-If an I/O error occurs
-
-
SecurityException
-In the case of the default provider, and a security manager are installed, the
checkWrite
method is invoked to Check write access to both the source and target file.
JAVA File.renameto moving files across file systems failed