In Java, Files.move(source, target) moves the file or folder source so that it is found at target afterwards. In the same folder that is a rename; with a different folder in target it is a move.
The target is the full new path, not a folder to drop into: to move into archive, use archive.resolve(source.getFileName()). If target exists the move fails unless you pass REPLACE_EXISTING.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.move(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Source, full target path, then options:
Java
Files.move(draft, dir.resolve("final.txt")); // rename
Files.move(report, archive.resolve(report.getFileName())); // into a folder
Files.move(tmp, config, StandardCopyOption.ATOMIC_MOVE); // all or nothing
Call
What it does
REPLACE_EXISTING
Overwrite an existing target file (or empty folder).
ATOMIC_MOVE
Move as one atomic file-system step, or fail.
File.renameTo(dest)
Old java.io way; returns only false on failure.
Since Java 7. Moving within one drive is a quick rename. Moving to another drive copies the data and deletes the source, which only works for files and empty folders.
Run with java MoveIntoFolder. The output is the same on Windows, macOS, and Linux.
archive already has report.txt
Left the inbox: true
Archived copy says: new
How It Works
Build the full target.archive.resolve(fresh.getFileName()) turns "the archive folder" into "archive/report.txt"; passing just archive would mean "replace the folder".
Name clash. An older report.txt is already in archive, so the first move throws FileAlreadyExistsException and nothing changes.
Replace on purpose. The second move overwrites the old report: the inbox copy is gone and the archive holds the new text.
Example 3 — Write to a Temp Name, Then Swap It in Atomically
Write the new settings to config.txt.tmp, then replace config.txt in one atomic step, so readers see either the old or the new file, never half of one.
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class MoveAtomicSave {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path config = dir.resolve("config.txt");
Files.write(config, "theme=light".getBytes(StandardCharsets.UTF_8));
Path tmp = dir.resolve("config.txt.tmp");
Files.write(tmp, "theme=dark".getBytes(StandardCharsets.UTF_8));
try {
Files.move(tmp, config, StandardCopyOption.ATOMIC_MOVE);
System.out.println("Atomic move done");
} catch (AtomicMoveNotSupportedException e) {
Files.move(tmp, config, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Fell back to a normal move");
}
System.out.println("config.txt: " + new String(Files.readAllBytes(config), StandardCharsets.UTF_8));
System.out.println("Temp file left over: " + Files.exists(tmp));
Files.delete(config);
Files.delete(dir);
}
}
Output
Run with java MoveAtomicSave. Output from the standard JDK on Windows, macOS, and Linux, where an atomic move replaces an existing target.
Atomic move done
config.txt: theme=dark
Temp file left over: false
How It Works
Never edit in place. The new content goes into a separate file first, so a crash while writing cannot damage config.txt.
One indivisible step. Both files are in the same folder, so ATOMIC_MOVE is possible and swaps the new file in; the existing config.txt is replaced on the standard JDK.
Plan B. If the platform cannot move atomically, AtomicMoveNotSupportedException triggers a normal move with REPLACE_EXISTING.
Frequently Asked Questions
Call Files.move(path, path.resolveSibling("newName.txt")). A move within the same folder is a rename.
Give the full new path: Files.move(file, folder.resolve(file.getFileName())). Passing only the folder would try to replace the folder itself.
It performs the move as a single atomic file-system operation, so other programs never see a half-finished state. If that is impossible, for example across drives, it throws AtomicMoveNotSupportedException.
Yes when it is only a rename on the same drive. When the entries would have to be copied to another drive, it fails with an IOException; copy the tree and delete the original instead.
File.renameTo returns false without a reason and its behaviour varies by platform. Files.move throws specific exceptions and supports REPLACE_EXISTING and ATOMIC_MOVE.