Java java.nio.file.Files move() Method
java.nio.file.Files.move()The method is the core method in the Java NIO (New I/O) package for moving or renaming files and directories. It provides, compared to traditionaljava.io.Fileclasses, more powerful and flexible file operation capabilities.
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 destination path |
| options | CopyOption... | Optional move options (varargs) |
Common CopyOption Options
StandardCopyOption.REPLACE_EXISTING: If the target file already exists, replace it.StandardCopyOption.ATOMIC_MOVE: Ensures that the move operation is atomic.LinkOption.NOFOLLOW_LINKS: Does not follow symbolic links.
Method Function Details
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);
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target);
File Renaming
The move method can also be used to rename files:
Example
Path source = Paths.get("C:/temp/oldname.txt");
Path target = Paths.get("C:/temp/newname.txt");
Files.move(source, target);
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);
Path targetDir = Paths.get("C:/temp2/mydir");
Files.move(sourceDir, targetDir);
Advanced Usage
Overwrite Existing Files
UsingREPLACE_EXISTINGoption to overwrite an existing destination 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);
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
Atomic Move
Ensure the move operation is atomic (either fully succeeds or fully 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);
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 occurs.FileAlreadyExistsException: The target file already exists and noREPLACE_EXISTINGoption is specified.DirectoryNotEmptyException: 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();
}
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-filesystem move: When moving files between different file systems, it is actually a copy + delete operation.
- Symbolic links: By default, symbolic links are followed, unless specified
LinkOption.NOFOLLOW_LINKS - Directory move restrictions: Cannot move a directory into itself or its subdirectory.
- File attributes: Move operations usually preserve the basic attributes of the file.
- 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 {
// Try atomic move
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
return true;
} catch (AtomicMoveNotSupportedException e) {
try {
// When 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;
}
}
try {
// Try atomic move
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
return true;
} catch (AtomicMoveNotSupportedException e) {
try {
// When 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);
}
}
}
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