1.配置文件test.properties:
test_123=admin
注:value 可用单引号,双引号,不用引号修饰
2.工具类PropertiesUtil:
package com.......test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
private static final String PROP_FILE = "test.properties";
private static Properties properties;
static {
properties = new Properties();
try {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROP_FILE);
BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));//解决读取properties文件中产生中文乱码的问题
assert (is != null);
properties.load(bf);
} catch (IOException e) {
throw new RuntimeException("读取" + PROP_FILE + "配置文件异常", e);
}
}
public static String getValue(String key) {
return properties.getProperty(key);
}
}
3.测试
@Test
public void test() {
String keyWord = "test_123";
String value = PropertiesUtil.getValue(keyWord);
System.out.println("value = " + value);
}