Java读取配置文件,如果外部文件不存在,则从jar内读取
Java读取配置文件
如果外部文件不存在,则从jar内读取,开发时配置文件路径:src/main/resources
// package me.muphy.util;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class PropertyUtils {
public static class IRefresher {
void refresh();
}
private final static Logger log = LoggerFactory.getLogger(PropertyUtils.class);
public static final String filename = "nuonuo.properties";
private static final Map<String, Map<String, String>> propertiesMap = new HashMap<>();
private static final List<IRefresher> propertyConfigs = new ArrayList<>();
public static void addRefresh(IRefresher config) {
if (config == null) {
return;
}
for (IRefresher propertyConfig : propertyConfigs) {
if (config == propertyConfig) {
return;
}
if (Objects.equals(config.fileName(), propertyConfig.fileName())) {
propertyConfigs.remove(propertyConfig);
}
}
propertyConfigs.add(config);
}
public static Map<String, String> getProperties() {
return getProperties(filename);
}
public static String getProperty(String key) {
if (key == null) {
return null;
}
Map<String, String> properties = getProperties();
String val = properties.get(key);
try {
log.debug("读取属性,key:" + key + ",value:" + val);
} catch (Exception e) {
e.printStackTrace();
}
return val;
}
public static String setProperty(String key, String value) {
if (key == null) {
return null;
}
Map<String, String> properties = getProperties();
properties.put(key, value);
refresh(filename, false);
return properties.get(key);
}
public static Map<String, String> refresh(String filename) {
return refresh(filename, true);
}
private static Map<String, String> refresh(String filename, boolean reloadFile) {
Map<String, String> properties = getProperties(filename, reloadFile);
for (IRefresher propertyConfig : propertyConfigs) {
if (Objects.equals(propertyConfig.fileName(), filename)) {
try {
propertyConfig.refresh();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
return properties;
}
public static Map<String, String> getProperties(String filename) {
return getProperties(filename, false);
}
private static Map<String, String> getProperties(String filename, boolean refresh) {
if (StringUtils.isEmpty(filename)) {
throw new RuntimeException("配置文件不能为空!");
}
if (!refresh && propertiesMap.containsKey(filename)) {
return propertiesMap.get(filename);
}
Map<String, String> map = new HashMap<>();
propertiesMap.put(filename, map);
Properties properties = new Properties();
File externalFile = new File(filename);
if (externalFile.exists() && externalFile.isFile()) {
try (FileInputStream inputStream = new FileInputStream(externalFile)) {
properties.load(inputStream);
if (!filename.contains("log.properties")) {
log.debug("加载了外部的配置文件: " + externalFile.getAbsolutePath());
}
} catch (IOException e) {
throw new RuntimeException("读取外部配置文件【" + filename + "】失败!", e);
}
} else {
try (InputStream inputStream = NuoNuoConfiguration.class.getClassLoader().getResourceAsStream(filename)) {
if (inputStream == null) {
try (InputStream is = NuoNuoConfiguration.class.getClassLoader().getResourceAsStream("default-" + filename)) {
if (is == null) {
throw new RuntimeException("找不到配置文件【" + filename + "】!请确保文件位于类路径的根目录下或JAR同级目录。");
}
properties.load(is);
if (!filename.contains("log.properties")) {
log.debug("加载了JAR内部的默认配置文件。");
}
} catch (IOException e) {
throw new RuntimeException("读取配置文件【" + filename + "】失败!", e);
}
} else {
properties.load(inputStream);
if (!filename.contains("log.properties")) {
log.debug("加载了JAR内部的配置文件。");
}
}
} catch (IOException e) {
throw new RuntimeException("读取配置文件【" + filename + "】失败!", e);
}
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = entry.getKey().toString();
String value = properties.getProperty(key);
if (value != null) {
map.put(key, new String(value.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
}
}
log.debug("加载到配置:" + JsonUtils.toJSONString(map));
return map;
}
}