批量修改文件名java

时间:2024-10-14 18:02:50

package test0715;

import java.io.File;

public class FileRename {
public static void main(String[] args) {
File file=new File("f:/java/test");
String dirPath=file.getAbsolutePath();
System.out.println("dirPath: "+dirPath);
if(file.isDirectory()){
File[] files=file.listFiles();
long starttime=System.currentTimeMillis();
for(File fileFrom:files){
String fromFile=fileFrom.getName();
System.out.println("fromFile before: "+fromFile);
String toFileName;
int index;
index=fromFile.indexOf("p");
fromFile=fromFile.substring(index+1);
System.out.println("fromFile after: "+fromFile);

if(index!=-1){
toFileName=dirPath+"\\"+fromFile;
System.out.println("toFileName: "+toFileName);
File toFile=new File(toFileName);
if(fileFrom.exists()&&!toFile.exists()){
fileFrom.renameTo(toFile);
}
}
}
long endtime=System.currentTimeMillis();
System.out.println("Time:"+new Long(endtime-starttime));
}
}
}