Java Programs›File Handling›Rename or Move a File

Rename or Move a File in Java

beginner·  File Handling  ·  File I/O

Problem

Renaming and moving are the same operation on a file system: both change the file's path, and on the same disk neither copies the content.

Rename report-draft.txt to report-final.txt, then move it into an archive folder and confirm its content is unchanged.

Input
report-draft.txt
Output
archive/report-final.txt

Java Program

Java
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; public class RenameMoveFile { public static void main(String[] args) throws IOException { Path draft = Paths.get("report-draft.txt"); Files.writeString(draft, "Q3 results"); Path renamed = draft.resolveSibling("report-final.txt"); Files.move(draft, renamed); // rename within the same folder System.out.println("Draft exists: " + Files.exists(draft) + ", final exists: " + Files.exists(renamed)); Path archive = Paths.get("archive"); Files.createDirectories(archive); Path target = archive.resolve(renamed.getFileName()); Files.move(renamed, target, StandardCopyOption.REPLACE_EXISTING); // move into another folder System.out.println("Now at: " + target.toString().replace('\\', '/')); System.out.println("Content kept: " + Files.readString(target)); Files.delete(target); Files.delete(archive); } }

Output

Draft exists: false, final exists: true Now at: archive/report-final.txt Content kept: Q3 results

Core Logic

Files.move handles both cases: a new name in the same folder is a rename, and a path in another folder is a move.

How It Works
  1. 1draft.resolveSibling("report-final.txt") builds a new path in the same folder.
  2. 2The first Files.move renames the file.
  3. 3Files.createDirectories makes sure the archive folder exists.
  4. 4The second Files.move, with REPLACE_EXISTING, moves it into the archive.
After the rename the draft no longer exists; after the move the file is at archive/report-final.txt with its text intact.
💡

Key Point: Without REPLACE_EXISTING, moving onto an existing file throws FileAlreadyExistsException.

Key Concepts

Files.move()resolveSibling()StandardCopyOption.REPLACE_EXISTING

Approach 2: Using File.renameTo()

Java
import java.io.File; import java.io.IOException; import java.nio.file.Files; public class RenameMoveFileIo { public static void main(String[] args) throws IOException { File draft = new File("notes-old.txt"); Files.writeString(draft.toPath(), "meeting notes"); File renamed = new File("notes-new.txt"); boolean ok = draft.renameTo(renamed); // returns false on failure instead of throwing System.out.println("renameTo succeeded: " + ok); System.out.println("old exists: " + draft.exists() + ", new exists: " + renamed.exists()); renamed.delete(); } }

Output

renameTo succeeded: true old exists: false, new exists: true

Core Logic

The older File.renameTo also renames files, but reports failure only with a false return value.

How It Works
  1. 1draft.renameTo(renamed) returns true on success.
  2. 2The old name no longer exists and the new one does.
  3. 3The demo file is deleted afterwards.
The program prints renameTo succeeded: true.
💡

Key Point: renameTo gives no reason when it fails and may not work across disks; prefer Files.move, which throws a descriptive exception.

Key Concepts

java.io.FilerenameTo()boolean result

Interview Questions

What does StandardCopyOption.ATOMIC_MOVE do?

It makes the move happen as a single, indivisible operation, so other programs never see a half-moved file. It fails if the file system can't guarantee that.

Can Files.move move a non-empty folder?

Yes on the same disk, where it's a rename. Across disks it fails, and you must copy the folder's contents recursively and then delete the original.

Learn the Concept

Related Programs