package com.yami.trading.common.util; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.util.Properties; import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.io.FileUrlResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.util.ResourceUtils; /** * @author JORGE * @description 配置配置文件 */ public class PropertiesReader { /** * 路径解析器 * 配置文件(*.yml|*.properties) */ private static ResourcePatternResolver pathResolver=new PathMatchingResourcePatternResolver(); /** * 字母序列 */ private static final String LETTERS="abcdefghijklmnopqrstuvwxyz"+"abcdefghijklmnopqrstuvwxyz".toUpperCase(); /** * 读取属性配置文件 * @param absolutePath 配置文件绝对路径 * @return 属性字典 */ public static Properties getPropertiesByRealPath(String absolutePath){ return getProperties(new File(absolutePath)); } /** * 读取属性配置文件 * @param fileUrl 配置文件路径 * @return 属性字典 */ public static Properties getProperties(URL fileUrl){ if(!ResourceUtils.isFileURL(fileUrl)) return null; return getProperties(new FileUrlResource(fileUrl)); } /** * 读取属性配置文件 * @param fileUri 配置文件路径 * @return 属性字典 */ public static Properties getProperties(URI fileUri){ URL url=null; try { url = fileUri.toURL(); } catch (MalformedURLException e) { e.printStackTrace(); return null; } if(!ResourceUtils.isFileURL(url)) return null; return getProperties(new FileUrlResource(url)); } /** * 读取属性配置文件 * @param confFile 配置文件 * @return 属性字典 */ public static Properties getProperties(File confFile){ if(!confFile.exists() || confFile.isDirectory()) return null; URL url=null; try { url = confFile.toURI().toURL(); } catch (MalformedURLException e) { e.printStackTrace(); return null; } if(!ResourceUtils.isFileURL(url)) return null; return getProperties(new FileUrlResource(url)); } /** * 判断是否为绝对路径 * @param resourcePath 资源路径 * @return 是否为绝对路径 */ public static boolean isAbsolutePath(String resourcePath){ if(null==resourcePath || resourcePath.trim().isEmpty()) return false; char firstChar=resourcePath.charAt(0); if(firstChar=='/' || firstChar=='\\' || firstChar==File.separatorChar) return true; int count=LETTERS.length(); for(int i=0;i