批量修改文件名
2017-08-28 16:46
197 查看
package com.zzu119.demo.test; import java.io.File; public class DoRename { public static final String FLAG = "[111]"; /** * 批量修改文件名 * @param file */ public static void doRename(File file){ try { if(file.exists()){ File[] files = file.listFiles(); if(null != files && files.length>0){ for (int i = 0; i < files.length; i++) { File entity = files[i]; String path = entity.getPath(); String prename = entity.getName(); File newEntity = null; if(prename.indexOf(FLAG)>=0){ String newName = path.replace(FLAG, ""); System.out.println(newName); newEntity = new File(newName); entity.renameTo(newEntity); } if(null==newEntity){ doRename(entity); //递归调用 }else{ doRename(newEntity); //递归调用 } } } }else{ System.out.println("file doesn't exist..."); } } catch (Exception e) { e.printStackTrace(); } } /** * main * @param args */ public static void main(String[] args) { File file1 = new File("E:/BaiduYunDownload/test"); doRename(file1); } }