批量按要求修改文件名
下载flv文件出现重名,需要一个个改很是麻烦,有了程序就就可以批量搞定了,只需设置几个参数
1 import java.io.File; 2 import java.util.LinkedList; 3 4 public class ChangeFileName{ 5 private String fileDir; 6 private String fileSuffix; 7 private String filename; 8 private String numstart; 9 private String numend; 10 private int volumeoffset=0; 11 12 public static void main(String[] args) { 13 long a = System.currentTimeMillis(); 14 ChangeFileName c =new ChangeFileName(); 15 c.setFileDir("F:\\player\\lyu"); 16 c.setFileSuffix(".mp4"); 17 c.setFilename("狼烟"); 18 c.setNumstart("("); 19 c.setNumend(")"); 20 c.setVolumeoffset(0); 21 c.getFileNameList(c); 22 System.out.println("耗时"+(System.currentTimeMillis() - a)+" millisecond"); 23 } 24 25 /** 26 * 递归查找当前给定目录下符合条件的文件,并返回所有合适文件的路径链表。 27 */ 28 public LinkedList<String> getFileNameList(ChangeFileName c) { 29 String strPath=c.getFileDir(); 30 31 File dir = new File(strPath); 32 File[] files = dir.listFiles(); 33 LinkedList<String> FileNames = new LinkedList<String>(); 34 String newFileName; 35 if (files == null) 36 return null; 37 for (int i = 0; i < files.length; i++) { 38 if (files[i].isDirectory()) { 39 getFileNameList(c); 40 } else { 41 newFileName = doChangeFileName(files[i],c); 42 if (newFileName != null) { 43 FileNames.add(newFileName); 44 System.out.println(newFileName); 45 } 46 } 47 } 48 return FileNames; 49 } 50 /** 51 * 检测file是否符合cf所列条件,如符合则按cf所列条件改变文件名 52 * @param file 53 * @param cf 54 * @return 55 */ 56 private String doChangeFileName(File file,ChangeFileName cf){ 57 String newfilename=cf.getFilename(); 58 String suffix=cf.getFileSuffix(); 59 String start=cf.getNumstart(); 60 String end=cf.getNumend(); 61 int offset=cf.getVolumeoffset(); 62 63 String filefullname = file.getAbsolutePath(); 64 int spritindex=filefullname.lastIndexOf("\\"); 65 String filepath=filefullname.substring(0, spritindex+1); 66 File newfile = file; 67 if (filefullname.endsWith(suffix)) { 68 int leftbracketindex=filefullname.indexOf(start)+1; 69 int rightbracketindex=filefullname.indexOf(end); 70 int volume=Integer.parseInt(filefullname.substring(leftbracketindex, rightbracketindex))+offset; 71 String volumestr=addZero(volume); 72 newfile=new File(filepath+newfilename+volumestr+suffix); 73 file.renameTo(newfile); 74 } 75 return newfile.getAbsolutePath(); 76 } 77 78 private String addZero(int volume){ 79 String volumeStr=""; 80 if(volume<10){ 81 volumeStr="0"+volume; 82 }else{ 83 volumeStr=String.valueOf(volume); 84 } 85 return volumeStr; 86 } 87 88 //getter and setter 89 public String getFileDir() { 90 return fileDir; 91 } 92 93 public void setFileDir(String fileDir) { 94 this.fileDir = fileDir; 95 } 96 97 public String getFileSuffix() { 98 return fileSuffix; 99 } 100 101 public void setFileSuffix(String fileSuffix) { 102 this.fileSuffix = fileSuffix; 103 } 104 105 public String getFilename() { 106 return filename; 107 } 108 109 public void setFilename(String filename) { 110 this.filename = filename; 111 } 112 113 public String getNumstart() { 114 return numstart; 115 } 116 117 public void setNumstart(String numstart) { 118 this.numstart = numstart; 119 } 120 121 public String getNumend() { 122 return numend; 123 } 124 125 public void setNumend(String numend) { 126 this.numend = numend; 127 } 128 129 public int getVolumeoffset() { 130 return volumeoffset; 131 } 132 133 public void setVolumeoffset(int volumeoffset) { 134 this.volumeoffset = volumeoffset; 135 } 136 137 138 139 }