以下实例演示了使用 File 类的 oldName.renameTo(newName) 方法来重命名文件:
- /*
- author by shouce.ren
- 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");
- }
- }
- }
以上代码运行输出结果为(执行该程序前你可以先创建 program.txt 文件):
- 已重命名