使用java批量修改文件名的一个小程序

由于工作需要,要将一个文件夹中的pdf文件的文件名改来和另一个文件夹中的bmp文件的文件名一致,其中两个文件夹中的相关文件的文件名中有一部分是相同的。

代码如下:

使用java批量修改文件名的一个小程序
import java.io.File;public class FileNameUtil { public static void main(String[] args) { String bmpPath="C:\Users\淡淡的定\Desktop\参照文件";//参照文件所在文件夹 String pdfPath="C:\Users\淡淡的定\Desktop\修改文件";//修改文件所在文件夹 String[] bmpList=new File(bmpPath).list();//获取文件夹内所有文件名 String[] pdfList=new File(pdfPath).list();//获取文件夹内所有文件名 for(String str:bmpList){//遍历参照文件 String bmpPath1 = str.substring(0,str.lastIndexOf("."));//获取无后缀的文件名 String bmpPath2 = str.substring(0,bmpPath1.lastIndexOf("产"));//获取文件名中的关键信息(文件名开始到“产”字) for(String str2:pdfList){//遍历修改文件 String pdfPath1 = str2.substring(0,str2.lastIndexOf("超"));//获取文件名中的关键信息(文件名开始到“超”字) if (pdfPath1.equals(bmpPath2)){//对比关键信息 File file=new File(pdfPath+"\"+str2);//要修改的文件的全路径名称 File newFile=new File(pdfPath+"\"+bmpPath1 + ".bmp");//修改后的文件的全路径名称 file.renameTo(newFile);//修改文件名 } } } }}

 

相关文章