420接收网闸文件服务(现场部署版)
Lius
2025-02-27 f06f695488a64dcc0945e282dffe120148f21dca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.mm.util;
 
import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
 
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLDecoder;
import java.util.Properties;
 
/**
 * @author clown
 * * @date 2023/12/26
 */
public class OperationProperties {
 
    /***
     * 动态获取配置文件内容
     * @param fileName 配置文件名称
     * @param key 配置文件key值
     * @return 返回对应key的值
     */
    public static String getProperties(String fileName,String key) {
        String ret= "";
        try {
            ret = PropertiesLoaderUtils.loadAllProperties(fileName).get(key).toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
 
    /***
     * 动态修改配置文件并返回修改值
     * @param fileName
     * @param key
     * @param value
     * @return value
     */
    public static String updateProperties(String fileName,String key,String value) {
        try {
            String filePath = PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();
            filePath = URLDecoder.decode(filePath, "utf-8");
            Properties props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
            props.setProperty(key, value);
            props.store(bw, "");
        } catch (Exception e) {
 
            e.printStackTrace();
        }
        return value;
    }
 
}