You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.4 KiB
Java
86 lines
2.4 KiB
Java
2 years ago
|
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<String, String> stringStringMap = new HashMap<>();
|
||
|
|
||
|
|
||
|
public static String getParam(String key){
|
||
|
return stringStringMap.get(key);
|
||
|
}
|
||
|
|
||
|
|
||
|
public static void loadConfig() throws IOException {
|
||
|
//从classpath 读取配置文件。将日志写入到result中。
|
||
|
Map<String, String> stringStringMaplocal = readParam("config.ini");
|
||
|
stringStringMap = stringStringMaplocal;
|
||
|
}
|
||
|
|
||
|
|
||
|
private static Map<String,String> readParam(String file) throws IOException {
|
||
|
Map<String,String> 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<String,String> 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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|