Batch file renaming java tool class

Write a java tool class that supports batch modification of the names of files and folders in the specified directory according to the number. For example: the numbers are 10001, 30541... According to the code and name matching table, the file names are modified in batches as Zhang San, Li Si.... Reduce tedious manual operations.
demo download address

 package com.zp.myjavademo.filecollection;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * author : zp
 * e-mail : zhangping1
 * time : 2019-08-29 13:17
 * desc :遍历文件目录,根据原文件名的客户号匹配json中客户姓名进行重命名
 * version: 1.0
 */
public class FileUtils {

    private static String jsonStr;
    private static FileNameBean bean = new FileNameBean();

    private static String jsonPathDir = "/Users/theappleofna/workspace/android_demo/MyTestCollection/MyJavaDemo/src/main/java/com/zp/myjavademo/filecollection/filename.json";
    private static String filePathDir = "/Users/theappleofna/workspace/test/files/";

    public static void main(String[] args) throws IOException {

        // 从目录读取json文件
        jsonStr = readJsonData(jsonPathDir);
//        System.out.println("得到当前路径json===" + jsonStr);

        // 解析。将匹配关系解析道json实体类
        bean = JSON.parseObject(jsonStr, FileNameBean.class);

        // 遍历得到所有文件数组
        File file = new File(filePathDir);
        String[] fileNameList = file.list();
        System.out.println("读取文件列表---" + Arrays.toString(fileNameList));

        // 进性重命名
        renameFile(fileNameList);
    }

    /**
     * 遍历文件目录,根据原文件名的客户号匹配json中客户姓名进性重命名
     *
     * @param fileName
     * @author zhangping
     */
    private static void renameFile(String[] fileName) {
        for (int i = 0; i < fileName.length; i++) {
            System.out.println("源文件名==" + fileName[i]);

            for (int n = 0; n < bean.getData().size(); n++) {
                if (bean.getData().get(n).getUSERSEQ().equals(fileName[i])) {
                    System.out.println("该客户索引==" + n);

                    File oldFileName = new File(filePathDir + fileName[i]);
                    File newFileName = new File(filePathDir + bean.getData().get(n).getCIFNAME() + "影像资料");
                    System.out.println("oldName===" + oldFileName + "\n newFileName==" + newFileName);

                    if (oldFileName.renameTo(newFileName)) {
                        System.out.println("重命名成功~~");
                    } else {
                        System.out.println("重命名失败!!!");
                    }
                }
            }
        }
    }

    public static String readJsonData(String pactFile) throws IOException {

        StringBuffer strbuffer = new StringBuffer();
        File myFile = new File(pactFile);// "D:"+File.separatorChar+"DStores.json"
        if (!myFile.exists()) {
            System.err.println("Can't Find " + pactFile);
        }
        try {
            FileInputStream fis = new FileInputStream(pactFile);
            InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");
            BufferedReader in = new BufferedReader(inputStreamReader);

            String str;
            while ((str = in.readLine()) != null) {
                strbuffer.append(str); // new String(str,"UTF-8")
            }
            in.close();
        } catch (IOException e) {
            e.getStackTrace();
        }
        // System.out.println("读取文件结束util");
        return strbuffer.toString();
    }
}


Guess you like

Origin blog.csdn.net/u011084603/article/details/100136012
Recommended