add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
59
60
package com.dobbinsoft.fw.support.component.dynamic;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
 
/**
 * ClassName: DynamicStorageFileSystemStrategy
 * Description: TODO
 *
 * @author: e-weichaozheng
 * @date: 2021-05-20
 */
public class DynamicStorageFileSystemStrategy implements DynamicStorageStrategy {
 
    private static final Logger logger = LoggerFactory.getLogger(DynamicStorageFileSystemStrategy.class);
 
    @Override
    public void write(String key, String value) {
        String filepath = System.getProperty("user.dir");
        String keyFile = filepath + "/" + key.replace(":", "/") + ".txt";
        try {
            File file = new File(keyFile);
            file.deleteOnExit();
            file.createNewFile();
            FileUtil.writeString(value, file, StandardCharsets.UTF_8);
        } catch (IOException e) {
            logger.error("[配置写入持久层] IO异常 keyPath=" + keyFile, e);
        }
    }
 
    @Override
    public String read(String key) {
        String filepath = System.getProperty("user.dir");
        String keyFile = filepath + "/" + key.replace(":", "/") + ".txt";
        try {
            return FileUtil.readString(new File(keyFile), StandardCharsets.UTF_8);
        } catch (IORuntimeException e) {
            logger.error("[配置读取持久层] IO异常 keyPath=" + keyFile);
            return null;
        }
    }
 
    @Override
    public boolean del(String key) {
        String filepath = System.getProperty("user.dir");
        String keyFile = filepath + "/" + key.replace(":", "/") + ".txt";
        File file = new File(keyFile);
        if (file.exists()) {
            file.deleteOnExit();
            return true;
        }
        return false;
    }
}