针对360加固后文件重命名
原始文件名:android_v2.2.3_app_223_1_jiagu_sign.apk
输出结果:android_v2.2.3_baidu.apk
public class Main {
private static String IN_FILE_PATH = "F:\\ApkVersion\\2.2.3\\";//目标文件地址
private static String OUT_FILE_PATH = "F:\\ApkVersion\\2.2.3\\";//输出文件地址
private static String APP_VERSION = "v2.2.3";//版本号
//渠道数组:顺序与加固渠道码一致
private static String[] channels = new String[]{
"baidu",
"360",
"qq",
"oppo",
"vivo"};
public static void main(String[] args) {
// write your code here
//批量重命名文件
updateFileName(IN_FILE_PATH,OUT_FILE_PATH);
}
private static void updateFileName(String dirPath,String outPath) {
File file = new File(dirPath);
//判断文件目录是否存在,且是文件目录
if (file.exists() && file.isDirectory()) {
//获取文件列表
File[] childFiles = file.listFiles();
String path = file.getAbsolutePath();
System.out.print("path =" + path);
for (File childFile : childFiles) {
//如果是文件
if (childFile.isFile()) {
String oldName = childFile.getName();
System.out.print("\noldName =" + oldName);
String[] splitResult = oldName.split("_");
String newName;
for (int i = 0; i < splitResult.length; i++) {
System.out.print("\nsplitResult =" + splitResult[i]);
}
int num = Integer.valueOf(splitResult[4]);
newName = "android_" + APP_VERSION + "_" + channels[num - 1] + ".apk";
ifDirExists(new File(outPath));
boolean update = childFile.renameTo(new File(OUT_FILE_PATH + newName));
if (update) {
System.out.print("\n第" + num + "个文件重命名成功" + "\n结果: " + childFile.getName());
}
}
System.out.print("\n文件重命名成功" + "\n结果: " + childFile.getName());
}
}
}
//判断文件夹是否存在
public static void ifDirExists(File dirFile) {
if (dirFile.exists()) {
if (dirFile.isDirectory()) {
System.out.println("\ndir exists");
} else {
System.out.println("\nthe same name file exists, can not create dir");
}
} else {
System.out.println("dir not exists, create it ...");
dirFile.mkdir();
}
}
}