一. PropertiesUtil 工具类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class PropertiesUtil {
private static Properties _prop = new Properties();
/**
* 读取配置文件
* @param fileName
*/
public static void readProperties(String fileName){
try {
InputStream in = PropertiesUtil.class.getResourceAsStream("/"+fileName);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
_prop.load(bf);
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 根据key读取对应的value
* @param key
* @return
*/
public static String getProperty(String key){
return _prop.getProperty(key);
}
}
二.配置文件:
三.测试类:
public class test {
/**
* @param args
*/
public static void main(String[] args) {
PropertiesUtil.readProperties("dzfp.properties");
System.out.println(PropertiesUtil.getProperty("SPBM"));
}
}
四. 运行结果: