Java Files move() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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
CallWhat it does
REPLACE_EXISTINGOverwrite an existing target file (or empty folder).
ATOMIC_MOVEMove 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.

Library declarations
Java
public static Path move(Path source, Path target, CopyOption... options) throws IOException

Parameters

ParameterDescription
sourceThe file or folder to move.
targetThe complete new path, including the file name.
optionsREPLACE_EXISTING and/or ATOMIC_MOVE. With ATOMIC_MOVE other options are ignored.

Return Value

Return ValueDescription
PathThe target path.

Compare with Similar Methods

Ways to relocate data:

MethodHow it differs
copy()Leaves the source in place.
delete()Removes without keeping a copy anywhere.
createTempFile()Often the first half of a write-then-move save.

Java Examples

Example 1 — Rename draft.txt to final.txt

A move inside the same folder is a rename. The old name disappears and the content travels with the new one.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class MoveRename {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path draft = dir.resolve("draft.txt");
        Files.write(draft, "ready to publish".getBytes(StandardCharsets.UTF_8));
        Path done = Files.move(draft, dir.resolve("final.txt"));
        System.out.println("Returned: " + done.getFileName());
        System.out.println("draft.txt exists: " + Files.exists(draft));
        System.out.println("final.txt says: " + new String(Files.readAllBytes(done), StandardCharsets.UTF_8));
        Files.delete(done);
        Files.delete(dir);
    }
}

How It Works

  1. Same parent, new name. Both paths live in the temp folder, so the operating system just renames the entry without copying any data.
  2. The return value is the target. done is the path final.txt, handy for the next step.
  3. The old name is gone. exists(draft) is false, while the content is readable under the new name.

Example 2 — Archive a Report, Replacing an Older One

Move inbox/report.txt into archive. The first try fails because archive already has a report.txt; REPLACE_EXISTING fixes that.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class MoveIntoFolder {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path inbox = Files.createDirectory(dir.resolve("inbox"));
        Path archive = Files.createDirectory(dir.resolve("archive"));
        Files.write(archive.resolve("report.txt"), "old".getBytes(StandardCharsets.UTF_8));
        Path fresh = inbox.resolve("report.txt");
        Files.write(fresh, "new".getBytes(StandardCharsets.UTF_8));
        Path target = archive.resolve(fresh.getFileName());
        try {
            Files.move(fresh, target);
        } catch (FileAlreadyExistsException e) {
            System.out.println("archive already has report.txt");
        }
        Files.move(fresh, target, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("Left the inbox: " + Files.notExists(fresh));
        System.out.println("Archived copy says: " + new String(Files.readAllBytes(target), StandardCharsets.UTF_8));
        Files.delete(target);
        Files.delete(archive);
        Files.delete(inbox);
        Files.delete(dir);
    }
}

How It Works

  1. Build the full target. archive.resolve(fresh.getFileName()) turns "the archive folder" into "archive/report.txt"; passing just archive would mean "replace the folder".
  2. Name clash. An older report.txt is already in archive, so the first move throws FileAlreadyExistsException and nothing changes.
  3. 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);
    }
}

How It Works

  1. Never edit in place. The new content goes into a separate file first, so a crash while writing cannot damage config.txt.
  2. 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.
  3. 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.

Next: Files.newBufferedReader()

Open a text file as a BufferedReader for line-by-line reading.

newBufferedReader() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful