java 文本配置文件的修改(小试)

需求:如何修改subtitle.conf 配置文件下的label_number 的值和 cg_color的值 ?

思路:创建一个一样的模板文件,进行替换

代码:

public static void main(String args[]) {

        /**
         * 仅供本地测试使用
         */
        String filePath="/Users/电脑名称/Desktop/subtitle.conf";//目标路径
        String templatePath="/Users/电脑名称/Desktop/subtitle.template";//模板路径

        Map<String,String> updateMap=new HashMap<String,String>();
        updateMap.put("position1","333");//替换内容
        updateMap.put("position2","444");//替换内容
        try {
            InputStream ins=new FileInputStream(templatePath);
            byte[] bytes= IOUtils.toByteArray(ins);
            String bufs=new String (bytes);
            updateContent(updateMap,bufs,filePath);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void updateContent(Map<String,String> layouts, String bufs,String filePath){

        try {
            Map<String,String> orig=new HashMap<String,String>();
            orig.putAll(layouts);
            for (Map.Entry entry:orig.entrySet()){
                bufs= StringUtils.replace(bufs,"#"+entry.getKey()+"#",entry.getValue().toString());
            }
            IOUtils.write(bufs,new FileOutputStream(filePath));//修改的目标文件路径
        } catch (IOException e) {
            e.printStackTrace();
        }
    }