Java 文件重命名

使用 File 类的 oldName.renameTo(newName) 方法来重命名文件

完整代码

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:/test.txt");
        if(oldName.renameTo(newName)) {
            System.out.println("已重命名");
        } else {
            System.out.println("Error");
        }
    }
}

结果输出

已重命名

在这里插入图片描述

发布了827 篇原创文章 · 获赞 112 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45743799/article/details/105179167