您的位置:首页 > 其它

批量修改文件名

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);
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: