The following example demonstrates the use of the oldName.renameTo(newName) method of the File class to rename a file:
/* author by w3cschool.cc Main.java */import java.io.File;public class Main { public static void main(String[] args) { File oldName = new File("C:/program.txt"); File newName = new File("C:/java.txt"); if(oldName.renameTo(newName)) { System.out.println("已重命名"); } else { System.out.println("Error"); } }}
The output result of the above code is (before executing the program, you can Create program.txt file):
已重命名
The above is the Java example-file renaming content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!