讀取.properties檔案的工具類
阿新 • • 發佈:2018-11-04
package com.javaTest; import java.io.File; import java.io.IOException; import java.util.Properties; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.util.ResourceUtils; public class Test { public static void main(String[] args) { String SHOPXX_PROPERTIES_PATH = "classpath:log4j.properties"; String resourceName = "log4j.appender.infoA"; String aa = getAttrValue(SHOPXX_PROPERTIES_PATH, resourceName); System.out.println(aa); } /** * 讀取properties配置檔案中屬性的值 * @param resourceLocation 檔名 比如:classpath:shopxx.properties * @param resourceName 資源名 * @return */ public static String getAttrValue(String resourceLocation, String resourceName){ Properties properties = null; try { File shopxxPropertiesFile = ResourceUtils.getFile(resourceLocation); properties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(shopxxPropertiesFile)); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } String attributeValue = properties.getProperty(resourceName); return attributeValue; } }