Java java.nio.file.Files move() Method

Java File Java java.nio.file.Files


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);

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);

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 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);

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);

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();
}

Notes

  1. Cross-filesystem move: When moving files between different file systems, it is actually a copy + delete operation.
  2. Symbolic links: By default, symbolic links are followed, unless specifiedLinkOption.NOFOLLOW_LINKS
  3. Directory move restrictions: Cannot move a directory into itself or its subdirectory.
  4. File attributes: Move operations usually preserve the basic attributes of the file.
  5. 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;
    }
}

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 File Java java.nio.file.Files

Other Extensions