分享

批量文件重命名代码

 以怪力乱神 2019-01-06
import java.io.File;

class Test {
public static void main(String[] args) {
File dir=new File("C:\\Users\\97392\\Desktop\\新建文件夹");
File[] files=dir.listFiles();



for(File file:files){
renameFile(file,file.getName()+".txt");
}



System.out.println("结束!");
}




//修改文件file的名称为newName
public static void renameFile(File file,String newName){
String path=file.getAbsolutePath();
String destPath=path.substring(0,path.lastIndexOf("\\"))+"\\"+newName;
boolean success=file.renameTo(new File(destPath));

if(success){
System.out.println(file.getName()+" --> "+newName);
}else{
System.out.println(file.getName()+" --> "+newName+"\t****重命名失败,可能新名称已经被占用!");
}
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多