读取.properties文件的工具类

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;
    }

}

猜你喜欢

转载自blog.csdn.net/bestxianfeng163/article/details/83416797