Java java.nio.file.Files move() Method
java.nio.file.Files.move() method is the core method in the Java NIO (New I/O) package for moving or renaming files and directories. It provides more powerful and flexible file operation capabilities than traditional java.io.File class.
Method Syntax
public
static
Path move
(
Path source, Path target, CopyOption...
options
)
throws
IOException
Parameter Description
| Parameter name | Type | Description |
|---|---|---|
| source | Path | The source file or directory path to move |
| target | Path | The target location path |
| options | CopyOption... | Optional move options (variable arguments) |
Commonly used CopyOption options
StandardCopyOption.REPLACE_EXISTING: If the target file already exists, replace itStandardCopyOption.ATOMIC_MOVE: Ensure the move operation is atomicLinkOption.NOFOLLOW_LINKS: Do not follow symbolic links
Detailed Method Functionality
Basic file move
The simplest move operation, moving a file from one location to another:
Example
Path source
=
Paths.
get
(
"C:/temp/source.txt"
)
;
Path target
=
Paths.
get
(
"C:/temp2/target.txt"
)
;
Files.
move
(
source, target
)
;
File renaming
The move method can also be used to rename a file:
Example
Path source
=
Paths.
get
(
"C:/temp/oldname.txt"
)
;
Path target
=
Paths.
get
(
"C:/temp/newname.txt"
)
;
Files.
move
(
source, target
)
;
Directory move
Move an entire directory (including its contents):
Example
Path sourceDir
=
Paths.
get
(
"C:/temp/mydir"
)
;
Path targetDir
=
Paths.
get
(
"C:/temp2/mydir"
)
;
Files.
move
(
sourceDir, targetDir
)
;
Advanced usage
Overwrite an existing file
Use REPLACE_EXISTING Option to overwrite an existing target file:
Example
Path source
=
Paths.
get
(
"C:/temp/source.txt"
)
;
Path target
=
Paths.
get
(
"C:/temp2/target.txt"
)
;
Files.
move
(
source, target, StandardCopyOption.
REPLACE_EXISTING
)
;
Atomic move
Ensure the move operation is atomic (either completely succeeds or completely fails):
Example
Path source
=
Paths.
get
(
"C:/temp/source.txt"
)
;
Path target
=
Paths.
get
(
"C:/temp2/target.txt"
)
;
Files.
move
(
source, target, StandardCopyOption.
ATOMIC_MOVE
)
;
Exception handling
move() The method may throw the following exceptions:
IOException: Thrown when an I/O error occursFileAlreadyExistsException: The target file already exists and the option was not specifiedREPLACE_EXISTINGOptionDirectoryNotEmptyException: Attempting to move a non-empty directory to a different file system
Recommended exception handling approach:
Example
try
{
Path source
=
Paths.
get
(
"C:/temp/source.txt"
)
;
Path target
=
Paths.
get
(
"C:/temp2/target.txt"
)
;
Files.
move
(
source, target, StandardCopyOption.
REPLACE_EXISTING
)
;
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"Error moving file: "
e.
getMessage
(
)
)
;
e.
printStackTrace
(
)
;
}
Notes
- Cross-file-system move: When moving files between different file systems, it is actually a copy + delete operation
- Symbolic links: Follows symbolic links by default, unless specified
LinkOption.NOFOLLOW_LINKS - Directory move restrictions: Cannot move a directory into itself or its own subdirectories
- File attributes: A move operation usually preserves the file's basic attributes
- Performance considerations: Moving within the same file system is usually much faster than moving across file systems
Practical Application Examples
Safely moving files
Example
public
static
boolean
safeMoveFile
(
Path source, Path target
)
{
try
{
// Attempt atomic move
Files.
move
(
source, target, StandardCopyOption.
ATOMIC_MOVE
)
;
return
true
;
}
catch
(
AtomicMoveNotSupportedException e
)
{
try
{
// If atomic move is not supported, use normal move + overwrite
Files.
move
(
source, target, StandardCopyOption.
REPLACE_EXISTING
)
;
return
true
;
}
catch
(
IOException
ex
)
{
System
.
err
.
println
(
"Failed to move file: "
ex.
getMessage
(
)
)
;
return
false
;
}
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
"Failed to move file: "
e.
getMessage
(
)
)
;
return
false
;
}
}
Batch moving files
Example
public
static
void
batchMoveFiles
(
Path sourceDir, Path targetDir,
String
filePattern
)
throws
IOException
{
try
(
DirectoryStream
<
Path
stream
=Files.
newDirectoryStream
(
sourceDir, filePattern
)
)
{
for
(
Path sourceFile
:
stream
)
{
Path targetFile
=
targetDir.
resolve
(
sourceFile.
getFileName
(
)
)
;
Files.
move
(
sourceFile, targetFile
)
;
}
}
}
Java java.nio.file.Files