zoukankan      html  css  js  c++  java
  • Java 实现文件批量重命名亲测可用(精简版)

    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身份证");
    }
    
    }
  • 相关阅读:
    作业任务03
    作业任务02
    作业任务01
    Shell脚本编程01-shell编程与规范与变量
    Linux网络服务05-----DNS域名解析服务(二)
    Linux网络服务05-----DNS域名解析服务(一)
    Linux网络服务13----PXE 高效能批量网络装机
    网络基础知识
    Nginx 入门
    shell 脚本须知
  • 原文地址:https://www.cnblogs.com/abc8023/p/6644690.html
Copyright © 2011-2022 走看看