修改配置文件工具类

13 篇文章 0 订阅

修改 yml,properties,ini 格式文件


import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.util.*;

/**
 * @Author: hekaikai
 * @Date: 2020/11/6 16:57
 */
public class UpdateYml {


    /**
     * 修改yml配置文件中的 reader.store.dir
     * @param file 配置文件
     * @param readerStoreDir 值
     */
    public static void update(File file,String readerStoreDir){
        Yaml yml =null;
        Map<String,Object> obj =null;
        try (FileInputStream in = new FileInputStream(file)){
            yml = new Yaml();
            obj = (Map)yml.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HashMap<String,Object> reader = (HashMap<String, Object>) obj.get("reader");
        HashMap<String,Object> store = (HashMap<String, Object>) reader.get("store");
        Object dir = store.get("dir");
        String s = String.valueOf(dir);
        store.put("dir", readerStoreDir);

        try (FileWriter writer = new FileWriter(file)) {
            //writer.write(yml.dump(obj));
            writer.write(yml.dumpAsMap(obj));
            //writer.write(yml.dumpAs(obj, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
            //可以自定义写入格式
            //writer.write(yml.dumpAs(obj, Tag.MAP, DumperOptions.FlowStyle.FLOW));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("修改yml文件出错了");
        }
    }

    /**
     * 修改项目的 properties文件
     * @param propertiesPath
     */
    public static void updatePropertiesJdbc(String propertiesPath){
        OutputStream fos=null;
        try {
            Properties props=new Properties();
            props.load(new FileInputStream(propertiesPath));
            props.setProperty("jdbc.url", "jdbc:mysql://127.0.0.1:3306/doc_standards?characterEncoding=UTF-8");
            props.setProperty("jdbc.username", "root");
            props.setProperty("jdbc.password", "123456");
            fos =new FileOutputStream(propertiesPath);
            props.store(fos, "修改jdbc");
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    /**
     * 更新配置文件 key=value 格式文件
     * @param configPath
     * @param map
     */
    public static void updateConfigFile(String configPath, Map<String, String> map) {
        Map<String, String> listMap = getListMap(new File(configPath));
        for (Map.Entry<String, String> entry : map.entrySet()) {
            listMap.put(entry.getKey(),entry.getValue());
        }

        File file=new File(configPath);
        try (
                OutputStream out=new FileOutputStream(file,false);
                OutputStreamWriter osw=new OutputStreamWriter(out,"utf-8");
                BufferedWriter bw=new BufferedWriter(osw);
                ){
            for (Map.Entry<String, String> entry : listMap.entrySet()) {
                bw.write(entry.getKey()+"="+entry.getValue());
                bw.newLine();
            }
            bw.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    private static Map<String,String> getListMap(File file){
        Map<String,String> map=new LinkedHashMap<>();
        List<String> stringList=new ArrayList<>();

        try(InputStream in=new FileInputStream(file);
            InputStreamReader inr=new InputStreamReader(in,"utf-8");
            BufferedReader br=new BufferedReader(inr);
        ){
            String str;
            while ((str=br.readLine())!=null){
                stringList.add(str);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        for (String line : stringList) {
            if(line.startsWith("#")) {
                continue;
            }
            if(isWhiteSpace(line)) {
                continue;
            }
            if(!line.contains("=")){
                continue;
            }
            int fd = line.indexOf("=");
            String key = line.substring(0, fd);
            String value = line.substring(fd + 1);
            map.put(key,value);
        }
        return map;

    }

    private static boolean isWhiteSpace(String line) {
        return line.replace(" ", "").equals("");
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值