package com.docus.sw; import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; public class Config { private static Map stringStringMap = new HashMap<>(); public static String getParam(String key){ return stringStringMap.get(key); } public static void loadConfig() throws IOException { //从classpath 读取配置文件。将日志写入到result中。 Map stringStringMaplocal = readParam("config.ini"); stringStringMap = stringStringMaplocal; } private static Map readParam(String file) throws IOException { Map params = new HashMap<>(); BufferedReader in = new BufferedReader(new FileReader(file)); String str; while ((str = in.readLine()) != null) { String[] split = str.split("="); params.put(split[0],split[1]); } in.close(); return params; } public static void updateConfig(String key,String value) throws IOException { if(stringStringMap==null){ throw new RuntimeException("先加载配置文件"); } stringStringMap.put(key,value); Files.write(Paths.get("config.ini"), new byte[0]); try (BufferedWriter writer = new BufferedWriter(new FileWriter("config.ini"))) { // 写入内容到文件 for(String keyN: stringStringMap.keySet()){ writer.write(keyN+"="+stringStringMap.get(keyN)); writer.newLine(); // 换行 } } catch (IOException e) { e.printStackTrace(); } } public static void updateConfig(Map keyvalue) throws IOException { if(stringStringMap==null){ throw new RuntimeException("先加载配置文件"); } stringStringMap.putAll(keyvalue); Files.write(Paths.get("config.ini"), new byte[0]); try (BufferedWriter writer = new BufferedWriter(new FileWriter("config.ini"))) { // 写入内容到文件 for(String keyN: stringStringMap.keySet()){ writer.write(keyN+"="+stringStringMap.get(keyN)); writer.newLine(); // 换行 } } catch (IOException e) { e.printStackTrace(); } } }