Rename or Move a File in Java
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.
Java Program
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
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.
- 1
draft.resolveSibling("report-final.txt")builds a new path in the same folder. - 2The first
Files.moverenames the file. - 3
Files.createDirectoriesmakes sure the archive folder exists. - 4The second
Files.move, withREPLACE_EXISTING, moves it into the archive.
archive/report-final.txt with its text intact.Key Point: Without REPLACE_EXISTING, moving onto an existing file throws FileAlreadyExistsException.
Key Concepts
Approach 2: Using File.renameTo()
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
Core Logic
The older File.renameTo also renames files, but reports failure only with a false return value.
- 1
draft.renameTo(renamed)returnstrueon success. - 2The old name no longer exists and the new one does.
- 3The demo file is deleted afterwards.
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
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.