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****重命名失败,可能新名称已经被占用!"); } } }
|