最近有个需求,需要对文件夹下某一格式的文件进行全部重命名,用到了java的File类,代码实现相当简单,留着以后再用
package com; import java.io.File; import java.io.FilenameFilter; public class Rename { private String fold=null; private File []files=null; private String fileType=null; public void findFile(String fold,final String type){ this.setFold(fold); this.setFileType(type); File dir=new File(fold); files=dir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { // TODO Auto-generated method stub return filename.endsWith("."+type); } }); } public void renameFiles(String fold, String type){ this.findFile(fold, type); int count=1; for (File file:this.files){ file.renameTo(new File(fold + "\"+ "1_"+count+"."+type));//命名格式为1_xx.wav count++; } } public String getFold() { return fold; } public void setFold(String fold) { this.fold = fold; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public static void main(String []args){ String fold="E:\data"; String type="wav"; Rename rn=new Rename(); rn.renameFiles(fold, type); } }