JAVA批量修改文件名

 import java.io.File;
/**
 * 文件 批量重命名
 * @author tiyoto
 *
 */
public class BatchRenamefile {
 //输出日志
 public static volatile boolean isDebug = false;
 
 public static void main(String[] args) {
  String root = "C:/Documents and Settings/Administrator/桌面/test_rename";//文件夹目录
  File [] fs = new File(root).listFiles();
  
  //String newName = "096";
  BatchRenamefile.isDebug = true;
  try {
   rename(fs);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * 批量 重命名 文件名
  * @param files 文件列表(文件夹或文件)
  * @param newName 新文件名
  * @throws Exception 可能的异常
  */
 public static void rename(File [] files ) throws Exception {
  if(files == null ){
   return;
  }
  for(int i=0 ; i< files.length ; i++){
   if(files[i] != null && files[i].exists()){
    File f = files[i];
    
    int lastIndex = f.getAbsolutePath().lastIndexOf(File.separator);
    //父目录
    String path = f.getAbsolutePath().substring(0 , lastIndex+1);
    if(f.isFile()){
     //文件,保持后缀名
     String extensions = f.getName().lastIndexOf(".") >0 ?
       ( f.getName().substring(f.getName().lastIndexOf(".")) ) : "";
     f.renameTo(new File(path+(Integer.parseInt(f.getName().replaceAll("[.][^.]+$", ""))-95)+ extensions));
     
    }
   }
  }
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值