自動批量修改文件名


需求

​ 作為一個android開發,經常需要將UI的切圖導入項目中,但是UI切圖文件通常是中文命名,而在android項目中,drawable文件名是不能用中文字符的,並且英文不能有大寫字母,空格,並且為了可讀性性,我們需要在單詞間用“_”隔開,所以我們需要將切圖文件名翻譯為我們需要的樣式

實現

  1. 申請開通百度通用翻譯API,得到"APP_ID和密鑰",這兩個是必需的

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

    申請成功后可以在開發者信息中查看

  2. 代碼實現

    一共有四個java文件

    image-20201028152758315

    MD5,HttpGet,TransApi為百度翻譯的demo文件,我們需要使用他們來使用百度翻譯的API

    https://fanyiapp.cdn.bcebos.com/api/demo/java.zip可以通過鏈接下載

    FileNameChange是通過調用百度翻譯的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. 代碼實現之后就是運行,我在這里發現在android studio中無法直接運行上面的代碼,通過查閱資料暫時無法解決。

    所以只能在java環境中運行該代碼了,如在idea中運行,如果有更好的方法可以在評論區留言,謝謝

結束

​ 雖然沒有找到最優的方法,但是還是能夠解決問題的,希望對大家有一點幫助

項目地址https://gitee.com/cl1016/file-name-change有需要可自取


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM