DocuController中使用keDD.properties里面的值,代码如下
这个只是内存中改写值,并没有实际在文件中改写值:
Properties sysConfig = new Properties();
InputStream inputStream = DocuController.class.getResourceAsStream("/keDD.properties");
try {
sysConfig.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
String result=sysConfig.getProperty("EXCEL_DIR");//
try {
System.out.println(new String(result.getBytes("ISO-8859-1"), "gbk"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
sysConfig.setProperty("EXCEL_DIR","c:");
System.out.println(sysConfig.get("EXCEL_DIR"));
内存中更改了值,实际也更改了值如下(2022年8月26日08:51:39)
Properties sysConfig = new Properties();
InputStream inputStream = Controller.class.getResourceAsStream("BaseAddr.properties");
try {
sysConfig.load(inputStream);
sysConfig.setProperty("LOOK_FOR_ROAD_ECX_DWORDPTR","xxxx");
sysConfig.setProperty("LOOK_FOR_ROAD_ECX_DWORDPTR1","xxxx1");
sysConfig.setProperty("LOOK_FOR_ROAD_ECX_DWORDPTR2","xxxx3");
//这里加true意思是往文本里追加内容,否则就是覆盖前面的内容
//flush的意思是把上面set的内容一次性追加写入文本
FileOutputStream fos = new FileOutputStream( "src/sample/BaseAddr.properties",true);
sysConfig .store(fos, "这里是注释");
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
output:
#123123
#Fri Aug 26 08:46:34 CST 2022
LOOK_FOR_ROAD_ECX_DWORDPTR2=xxxx3
LOOK_FOR_ROAD_ECX_DWORDPTR1=xxxx1
LOOK_FOR_ROAD_ECX_DWORDPTR=xxxx
2022年8月26日23:51:55 上面的不好使,先用下面的操作,先写入文本,然后要使用的时候再使用上面的Properties
public static void storeValue(Map<String,String>keyvalue,String comment) throws IOException {
FileWriter fw=new FileWriter("src/sample/BaseAddr.properties",true);
BufferedWriter bw = new BufferedWriter(fw, 1024);
bw.write("#"+comment+"\n");
for (Map.Entry entry:keyvalue.entrySet()){
bw.write(entry.getKey()+" = "+entry.getValue()+"\n");
bw.flush();
}
bw.close();
System.out.printf("写入数据成功:"+keyvalue.toString()+"\n");
}