Rename file or directory in Java
Description
The following code shows how to rename file or directory.
Example
/*www. j ava 2 s.c o m*/
import java.io.File;
public class Main {
public static void main(String[] args) {
File oldName = new File("C:/s.txt");
File newName = new File("C:/d.txt");
if (oldName.renameTo(newName)) {
System.out.println("renamed");
} else {
System.out.println("Error");
}
}
}
The code above generates the following result.