/*
Rename file or directory
This Java example shows how to rename file or directory using renameTo
method of Java File class.
*/
import java.io.*;
public class RenameFileDirectory {
public static void main(String[] args) {
//create source File object
File oldName = new File("C://FileIO//source.txt");
//create destination File object
File newName = new File("C://FileIO//destination.txt");
/*
* To rename a file or directory, use
* boolean renameTo(File destination) method of Java File class.
*
* This method returns true if the file was renamed successfully, false
* otherwise.
*/
boolean isFileRenamed = oldName.renameTo(newName);
if(isFileRenamed)
System.out.println("File has been renamed");
else
System.out.println("Error renaming the file");
}
}
/*
Output would be
File has been renamed
*/