Automatically modify file names in batches

demand

​ As an android development, it is often necessary to import UI cuts into the project, but UI cuts files are usually named in Chinese, and in the android project, the drawable file name cannot use Chinese characters, and English cannot have uppercase letters. Spaces, and for readability, we need to separate words with "_", so we need to translate the file name of the cut image to the style we need

achieve

  1. Apply to open Baidu Universal Translation API, get "APP_ID and key", these two are required

    https://api.fanyi.baidu.com/doc/12

    After the application is successful, you can view it in the developer information

  2. Code

    There are four java files

    MD5, HttpGet, and TransApi are the demo files translated by Baidu. We need to use them to use Baidu's translation API

    https://fanyiapp.cdn.bcebos.com/api/demo/java.zip can be downloaded through the link

    FileNameChange is a file that realizes our specific needs by calling the files in the Baidu translated demo

    FileNamechange.java

    import org.json.JSONArray;
    import org.json.JSONObject;
    
    import java.io.File;
    import java.io.UnsupportedEncodingException;
    
    /**
     * Created with IntelliJ IDEA.
     *
     * @Auther: jayclin
     * @Date: 2020/09/30/11:53
     * @Description:
     */
    public class FileNameChange {
          
          
        private static final String APP_ID = "";//使用自己的APP_ID
        private static final String SECURITY_KEY = "";//同上
    
        /**
         * 从json字符串中提取英文翻译
         * @param jsonData
         * @return
         */
        private static String parse(String jsonData) {
          
          
            try {
          
          
                JSONObject jsonObject = new JSONObject(jsonData);
                JSONArray results = jsonObject.getJSONArray("trans_result");
                JSONObject result = results.getJSONObject(0);
                return (String) result.get("dst");
            } catch (Exception e) {
          
          
                e.printStackTrace();
                return "";
            }
        }
    
        /**
         * 将path路径下所有的文件名改为英文
         * @param path
         * @throws UnsupportedEncodingException
         * @throws InterruptedException
         */
        public static void changeFileName(String path) throws UnsupportedEncodingException, InterruptedException {
          
          
            TransApi api = new TransApi(APP_ID, SECURITY_KEY);
            File file = new File(path);
            File[] list = file.listFiles();
    
            if (file.exists() && file.isDirectory()) {
          
          
                for (int i = 0; i < list.length; i++) {
          
          
                    String name = list[i].getName();
                    int index = name.indexOf(".");
                    String name2 = name.substring(0, index);//文件名前缀
                    int index2 = name.lastIndexOf(".");
                    String name3 = name.substring(index2);
                    Thread.sleep(1000);//百度API的免费版本1秒只能有一个接入
                    String result = api.getTransResult(name2, "auto", "en");
                    String enString = parse(result);
                    enString=enString.replace("-", "");
                    String newName = enString.replace(' ', '_') + name3;
                    //重命名
                    File dest = new File(path + "/" + newName.toLowerCase());
                    list[i].renameTo(dest);
                    System.out.println(dest.getName());
                }
            }
        }
    
        /**
         * 改变android的Drawable文件夹的不同dp的所有文件名,只需将该drawable文件路径传入
         * @param path
         */
        public static void changeDrawableFile(String path){
          
          
            File file=new File(path);
            File[] list=file.listFiles();
            if (file.exists()&&file.isDirectory()){
          
          
                for (int i=0;i<list.length;i++){
          
          
                    String name=list[i].getName();
                    String newPath=path+"/"+name;
                    try {
          
          
                        changeFileName(newPath);
                    } catch (UnsupportedEncodingException e) {
          
          
                        e.printStackTrace();
                    } catch (InterruptedException e) {
          
          
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
          
          
            changeDrawableFile("/Users/mac/Downloads/更多");
        }
    }
    
    
  3. After the code is implemented, it is run. I found here that the above code cannot be run directly in android studio, and it cannot be solved temporarily by consulting the information.

    So the code can only be run in the java environment, such as running in idea, if there is a better way, you can leave a message in the comment area, thank you

End

​ Although I haven't found the best way, I can still solve the problem. I hope it will help you a little bit.

Project address https://gitee.com/cl1016/file-name-change can be picked up if necessary

Guess you like

Origin blog.csdn.net/weixin_43435453/article/details/109298766