package com.cmge.utils; import java.io.*; import java.util.*; import java.util.Map.Entry; import com.cmge.db.EpDB; import com.cmge.db.SysEmployeeDB; public class RenameImg { EpDB db = new EpDB(); private Map<String, String> map; public RenameImg() { this.map = new HashMap<String, String>(); } public Map<String, String> getMap() { return map; } // 添加新的替换规则(字符串替换) public RenameImg addRegulation(String oldStr, String newStr) { this.map.put(oldStr, newStr); return this; } /** * 批量重命名 * * @param path * @param replacementChain */ public void multiRename(String path, RenameImg replacementChain) { File file = new File(path); boolean isDirectory = file.isDirectory(); /** 如果不是文件夹,就返回* */ if (!isDirectory) { System.out.println(path + "不是一个文件夹!"); return; } String[] files = file.list(); File f = null; String filename = ""; String oldFileName = ""; // 之前的名字 /** 循环遍历所有文件* */ for(String fileName : files){ oldFileName = fileName; Map<String, String> map = replacementChain.getMap(); for (Entry<String, String> entry : map.entrySet()) { fileName = fileName.replace(entry.getKey(), entry.getValue()); } f = new File(path + "\" + oldFileName); //输出地址和原路径保持一致 f.renameTo(new File(path + "\" + fileName)); } System.out.println("恭喜,批量重命名成功!"); } public void Rename(String path) { File file = new File(path); boolean isDirectory = file.isDirectory(); /** 如果不是文件夹,就返回* */ if (!isDirectory) { System.out.println(path + "不是一个文件夹!"); return; } String[] files = file.list(); File f = null; String oldFileName = ""; // 之前的名字 String name=""; String badge=""; /** 循环遍历所有文件* */ for (String fileName : files) { oldFileName = fileName; //Map<String, String> map = replacementChain.getMap(); name=oldFileName.substring(0, oldFileName.lastIndexOf(".")); badge=getEmpBadge(oldFileName.substring(0, oldFileName.lastIndexOf("."))); fileName = fileName.replace(name, badge+"_004"); f = new File(path + "\" + oldFileName); // 输出地址和原路径保持一致 f.renameTo(new File(path + "\" + fileName)); } System.out.println("恭喜,批量重命名成功!"); } public void Insert(String path) { File file = new File(path); boolean isDirectory = file.isDirectory(); /** 如果不是文件夹,就返回* */ if (!isDirectory) { System.out.println(path + "不是一个文件夹!"); return; } String[] files = file.list(); File f = null; String sql=""; String badge=""; /** 循环遍历所有文件* */ for (String fileName : files) { badge=fileName.substring(0, fileName.lastIndexOf(".")).substring(0,5); sql="insert into lsq_fujian (badge,type,optdate) select '"+badge+"','"+001+"',sysdate from dual "; System.out.println(sql); db.updateBySql(sql); } System.out.println("恭喜,数据插入成功!"); } public String getEmpBadge(String name ){ String sql="select badge from tf_employee where name='"+name+"'"; String m= db.getValue(sql); return m; } public static void main(String[] args) { RenameImg replacementChain = new RenameImg(); //第一步把文件命名规则化 //replacementChain.addRegulation("(", "").addRegulation(")", ""); //replacementChain.multiRename("D:\HR部门资料\HR锐明\人员附件\0简历", replacementChain); //第二步将文件命名有姓名改为工号_001 //replacementChain.Rename("D:\HR部门资料\HR锐明\人员附件\001身份证"); replacementChain.Insert("D:\HR部门资料\HR锐明\人员附件\001身份证"); } }