Android国际化string资源,从一个项目复制到另一个项目,脚本代码

public class MyCopyTest {

    public static List<String> myCopyStrTitle = new ArrayList<>();

    /**
     * 这里填写源项目的路径和目标项目的路径
     */
    public static final String sourcePathStr = "E:\\Code2018\\Git_ApeSeneschal3.0_odm_new\\20180322\\ApeSeneschal3.0\\app\\src\\main\\res";
    public static final String dirPathStr = "E:\\Code2018\\Git_ApeSeneschal3.0_sugar_ui\\20190529\\ApeSeneschal3.0\\app\\src\\main\\res";


    public static void main(String[] args) {
        initCopyStrs();
        File sourceFile = new File(sourcePathStr);
        File[] sourceFiles = sourceFile.listFiles();
        if (null != sourceFiles && sourceFiles.length != 0) {
            for (File f : sourceFiles) {
                String name = f.getName();
                if (isStrStartWith("value", name)) {
                    File sourceCopyStrFile = new File(sourcePathStr + File.separator + name + File.separator + "strings.xml");
                    File dirCopyStrFile = new File(dirPathStr + File.separator + name + File.separator + "strings.xml");
                    if (sourceCopyStrFile.exists() && dirCopyStrFile.exists()) {
                        copyStr(sourcePathStr + File.separator + name + File.separator + "strings.xml"
                                ,dirPathStr + File.separator + name + File.separator + "strings.xml");
                    }
                }
            }

        }
        System.out.print("\n" + "---------------------------Finish----------------------------");
    }

    /**
     * 这里填写字要复制的符串的id
     * (建议分批复制,写太多顺序会乱)
     */
    private static void initCopyStrs() {
        myCopyStrTitle.add("adv_enter");
        myCopyStrTitle.add("adv_battery_descp");
        myCopyStrTitle.add("adv_default_app_descp");
        myCopyStrTitle.add("adv_notification_descp");
        myCopyStrTitle.add("adv_permission_descp");
        myCopyStrTitle.add("adv_traffic_descp");
        myCopyStrTitle.add("adv_update_descp");
        myCopyStrTitle.add("adv_virus_descp");
        myCopyStrTitle.add("adv_clean_descp");
        myCopyStrTitle.add("adv_boot_optimizer");
        myCopyStrTitle.add("adv_mobile_slimming");
        myCopyStrTitle.add("adv_mem_speed_up_des");
        myCopyStrTitle.add("adv_mem_speed_up");
        myCopyStrTitle.add("adv_mem_release_size");
        myCopyStrTitle.add("adv_mem_mem_release");
        myCopyStrTitle.add("text_garbage_clean_tip");
        myCopyStrTitle.add("cleanup_garbage_cleaned");
        myCopyStrTitle.add("cleanup_memory_released");
        myCopyStrTitle.add("cleanup_memory_ram_released");
        myCopyStrTitle.add("cleanup_optimal");
        myCopyStrTitle.add("cleanup_phone_is_clean");
        myCopyStrTitle.add("cleanup_please_keep_on");
        myCopyStrTitle.add("str_virus_phone_safe");
        myCopyStrTitle.add("str_virus_safe_state");
        myCopyStrTitle.add("str_virus_secure");
        myCopyStrTitle.add("str_virus_no_threats");
        myCopyStrTitle.add("one_tap_result_n_items");
        myCopyStrTitle.add("one_tap_result_optimized");
//        myCopyStrTitle.add("<string name=\"" + "" + "\">");
    }

    public static void copyStr(String sourceStrPath, String dirStrPath) {
        System.out.print("\n" + "copy start sourceStrPath:" + sourceStrPath);
        System.out.print("\n" + "copy start dirStrPath:" + dirStrPath);
        File sourceStrFile = new File(sourceStrPath);
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(sourceStrFile));
            String tempStr = null;
            while ((tempStr = bufferedReader.readLine()) != null) {
                for(String cpStr : myCopyStrTitle) {
                    if(tempStr.contains("<string name=\"" + cpStr + "\">")) {
                        System.out.print("\n" + "tempStr:" + tempStr);
                        insertStr(tempStr,dirStrPath);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public static void insertStr(String tempStr, String dirStrPath) {
        File sourceStrFile = new File(dirStrPath);
        RandomAccessFile randomAccessFile = null;
        if(null != sourceStrFile && sourceStrFile.exists()) {
            try {
                randomAccessFile = new RandomAccessFile(dirStrPath,"rw");
                String randomAccStr = null;
                int currentLine = 0;
                long insertPosi = 0;
                while ((randomAccStr = randomAccessFile.readLine()) != null) {
                    if(randomAccStr.startsWith("</resources>")) {
                        System.out.print("\n" + "randomAccStr:" + randomAccStr);
                        insertPosi = randomAccessFile.getFilePointer() - randomAccStr.length();
                        break;
                    }
                    currentLine++;
                }
                randomAccessFile.seek(insertPosi);
                randomAccessFile.write((tempStr).getBytes());
                randomAccessFile.write((System.getProperty("line.separator") + "</resources>").getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException e1) {
                    }
                }
            }
        }
    }

    public static boolean isStrStartWith(String cantainStr, String allStr) {
        if (null == cantainStr && cantainStr.isEmpty()) {
            return false;
        }
        if (null == allStr && allStr.isEmpty()) {
            return false;
        }
        return allStr.indexOf(cantainStr) == 0;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值