Java Reference
In-Depth Information
The method expects two arguments and there is the possibility to add more arguments that specify options
for the operation. The first argument is a
Path
object specifying the source path and the second argument
is a
Path
object specifying the target or destination. It is easy to misunderstand the target path. It is not
the path for the directory that contains the item after the move operation, but the path specification for
the item after it has been moved. The optional third or fourth arguments are values of type
CopyOption
.
Only two possible
CopyOption
values are applicable to the
move()
method and these are defined in the
java.nio.file.StandardCopyOption
enumeration:
•
REPLACE_EXISTING
, which indicates the target file specified by the first argument should be re-
placed if it exists and is not a non-empty directory
•
ATOMIC_MOVE
, which specifies that the move is executed as an atomic operation.
An
atomic operation
is an operation that cannot be interrupted by another thread of execution. If you
specify
ATOMIC_MOVE
, any other
CopyOption
values that you specify are ignored, so essentially you specify
the optional third argument to the
move()
method as either
REPLACE_EXISTING
or
ATOMIC_MOVE
.
If you specify anything other than the two possible options, the method throws an exception of type
Un-
supportedOperationException
. If the copy operation causes a file or directory to be replaced and you
have not specified the
REPLACE_EXISTING
option, an exception of type
FileAlreadyExistsException
is
thrown. If you specify
ATOMIC_MOVE
and the file cannot be moved as an atomic operation, an exception of
type
java.nio.file.AtomicMoveNotSupportedException
is thrown.
Here's how you could move a file:
Path sourcePath = Paths.get("D:/Temp1/output.txt");
Path destinationPath = Paths.get("D:/Temp2/output.txt");
try {
sourcePath.moveTo(destinationPath);
} catch(NoSuchFileException e) {
System.err.println("Move failed." + e);
} catch(FileAlreadyExistsException e) {
System.err.println("File already exists." + e);
} catch(IOException e) {
System.err.println("I/O error." + e);
}
This moves the
output.txt
file from the
Temp1
directory on the
D:
drive to
Temp2
. If the file does not
exist in the
Temp1
directory, an exception of type
NoSuchFileException
is thrown. If the file already exists
in
Temp2
,
FileAlreadyExistsException
is thrown. You can prevent this exception from being thrown by
specifying
REPLACE_EXISTING
as the third argument.
If you want to execute this fragment in a program, first create the directories
D:\Temp1
and
D:\Temp2
and create the file
output.txt
in the
Temp1
directory. (Of course, you could copy any file to the
Temp1
dir-
ectory and change the code accordingly). If you execute the fragment twice, it throws an exception of type
NoSuchFileException
.
Renaming a File or Directory
A
move()
operation renames a file or directory if the parent paths of the source and destination paths are
the same, and both reference a file or both reference a directory. Here's how you could rename the directory
D:\Temp1
to
D:\Temp2
: