将自身部分功能打成jar包时,需要动态从外部读取配置文件。
此工具类优先从项目路径下读取配置文件,读取不到时从classpath获取配置文件。
使用方式:
开发时直接读取项目资源目录下的 properties即可
打成jar包使用时,将配置文件与jar包放置在同一目录
代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtils {
/**
* properties
*/
private static Properties properties = null;
/**
* 根据key获取value值
*
* @param key
* @return
*/
public static String getValue(String key) {
if (properties == null) {
properties = loadConfProperties();
}
String value = properties.getProperty(key);
System.out.print