自动批量修改文件名

​ 作为一个android开发,经常需要将UI的切图导入项目中,但是UI切图文件通常是中文命名,而在android项目中,drawable文件名是不能用中文字符的,并且英文不能有大写字母,空格,并且为了可读性性,我们需要在单词间用“_”隔开,所以我们需要将切图文件名翻译为我们需要的样式

实现

申请开通百度通用翻译API,得到"APP_ID和密钥",这两个是必需的

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

申请成功后可以在开发者信息中查看

代码实现

一共有四个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 + "http://www.likecs.com/" + 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+"http://www.likecs.com/"+name; try { changeFileName(newPath); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { changeDrawableFile("/Users/mac/Downloads/更多"); } }

代码实现之后就是运行,我在这里发现在android studio中无法直接运行上面的代码,通过查阅资料暂时无法解决。

所以只能在java环境中运行该代码了,如在idea中运行,如果有更好的方法可以在评论区留言,谢谢

结束

​ 虽然没有找到最优的方法,但是还是能够解决问题的,希望对大家有一点帮助

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgyfzx.html