Files
moss-sdk/src/main/java/com/lakala/moss/util/PropertiesUtil.java
2025-09-02 13:55:32 +08:00

194 lines
7.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.lakala.moss.util;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* @author Stephen yu
* @description: 配置文件util
* @date 2025-02-21
*/
public class PropertiesUtil {
/**
* 私有化构造函数,防止类被实例化
*/
private PropertiesUtil(String filePath, String encode) {
System.out.println("PropertiesUtil init filePath: " + filePath);
File file = new File(filePath);
File[] array = file.listFiles();
for(File childfile : array) {
String childFileName = childfile.getName();
if(!childFileName.endsWith("properties")) continue;
loadConfigProperties(childFileName, encode);
}
}
/**
* 存放配置文件的所有的key-value
* 以这种方式存储是为了防止多个配置文件间存在key冲突
*/
private Map<String, HashMap<String,String>> allParam = new HashMap<String, HashMap<String, String>>();
/**
* 默认的properties文件名
*/
private static final String DEFAULT_PROPERTIES_NAME = "moss-sdk.properties";
/**
* 解析properties文件默认编码
*/
private static final String DEFAULT_ENCODE = "UTF-8";
/**
* 解析properties文件编码
*/
private static String fileEncode = null;
/**
* properties文件所在的地址
*/
private static final String propertiesParentPath = "/app";
/**
* 根据文件名称-key返回相应key的值
* 文件名默认为config.properties文件编码默认为UTF-8
*/
public static Integer getIntegerByKey(String key) {
return Integer.parseInt(getStringByKey(DEFAULT_PROPERTIES_NAME, key, DEFAULT_ENCODE));
}
/**
* 根据文件名称-key返回相应key的值文件编码默认为UTF-8
* @param fileName 文件名
* @param key properties文件中key的名称
*/
public static Integer getIntegerByKey(String fileName, String key) {
return Integer.parseInt(getStringByKey(fileName, key, DEFAULT_ENCODE));
}
/**
* 根据文件名称-key返回相应key的值
* @param fileName 文件名
* @param key properties文件中key的名称
* @param encode 文件编码
*/
public static Integer getIntegerByKey(String fileName, String key, String encode) {
return Integer.parseInt(getStringByKey(fileName, key, encode));
}
/**
* 根据文件名称-key返回相应key的值
* 文件名默认为config.properties文件编码默认为UTF-8
*/
public static Boolean getBooleanByKey(String key) {
return Boolean.parseBoolean(getStringByKey(DEFAULT_PROPERTIES_NAME, key, DEFAULT_ENCODE));
}
/**
* 根据文件名称-key返回相应key的值文件编码默认为UTF-8
* @param fileName 文件名
* @param key properties文件中key的名称
*/
public static Boolean getBooleanByKey(String fileName, String key) {
return Boolean.parseBoolean(getStringByKey(fileName, key, DEFAULT_ENCODE));
}
/**
* 根据文件名称-key返回相应key的值
* @param fileName 文件名
* @param key properties文件中key的名称
* @param encode 文件编码
*/
public static Boolean getBooleanByKey(String fileName, String key, String encode) {
return Boolean.parseBoolean(getStringByKey(fileName, key, encode));
}
/**
* 根据文件名称-key返回相应key的值
* 文件名默认为config.properties文件编码默认为UTF-8
*/
public static Long getLongByKey(String key) {
return Long.parseLong(getStringByKey(DEFAULT_PROPERTIES_NAME, key, DEFAULT_ENCODE));
}
/**
* 根据文件名称-key返回相应key的值文件编码默认为UTF-8
* @param fileName 文件名
* @param key properties文件中key的名称
*/
public static Long getLongByKey(String fileName, String key) {
return Long.parseLong(getStringByKey(fileName, key, DEFAULT_ENCODE));
}
/**
* 根据文件名称-key返回相应key的值
* @param fileName 文件名
* @param key properties文件中key的名称
* @param encode 文件编码
*/
public static Long getLongByKey(String fileName, String key, String encode) {
return Long.parseLong(getStringByKey(fileName, key, encode));
}
/**
* 根据文件名称-key返回相应key的值
* 文件名默认为config.properties文件编码默认为UTF-8
*/
public static String getStringByKey(String key){
return getStringByKey(DEFAULT_PROPERTIES_NAME, key, DEFAULT_ENCODE);
}
/**
* 根据文件名key返回value值文件编码默认为UTF-8
*/
public static String getStringByKey(String fileName, String key) {
return getStringByKey(fileName, key, DEFAULT_ENCODE);
}
/**
* 根据文件名key编码返回value值
*/
public static String getStringByKey(String fileName, String key, String encode) {
fileEncode = encode;
return PropertiesUtilHolder.instance.allParam.get(fileName).get(key);
}
/**
* 静态内部类,单例模式,保证只有一个实例变量
*/
private static class PropertiesUtilHolder {
private static PropertiesUtil instance;
static {
String parentPath = propertiesParentPath;
if (!Files.exists(Paths.get(parentPath))) {
parentPath = PropertiesUtil.class.getClassLoader()
.getResource(DEFAULT_PROPERTIES_NAME).getPath().replace(DEFAULT_PROPERTIES_NAME, "");
}
instance = new PropertiesUtil(parentPath, fileEncode);
}
}
/**
* 加载配置文件,需要进行加锁
*/
private void loadConfigProperties(String fileName, String encode) {
InputStream in = null;
try {
Properties p = new Properties();
File file = new File(fileName);
if(file.exists()) {
p.load(new FileInputStream(file));
} else {
in = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
// 解决中文乱码
BufferedReader bf = new BufferedReader(new InputStreamReader(in, encode));
p.load(bf);
}
Set<Entry<Object, Object>> allKey = p.entrySet();
HashMap<String, String> paramMap = new HashMap<String, String>();
for (Entry<Object, Object> entry : allKey) {
paramMap.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
allParam.put(fileName, paramMap);
System.out.println("loadConfig " + fileName + " keys: " + allParam.keySet());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(in != null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}