一、配置文件config.properties是放在src根目录下的:例如我的是
/PropertiesTest/src/com/xuliugen/project/type.properties
配置文件中的内容如下:
left=com.sunny.project.LeftHair
right=com.sunny.project.RightHair
in=com.sunny.project.InHair
读取配置文件中的代码如下:
public class PropertiesReader {
public static void main(String[] args) {
new PropertiesReader().getProperties();
}
public Map<String, String> getProperties() {
Properties props = new Properties();
Map<String, String> map = new HashMap<String, String>();
try {
InputStream in = getClass().getResourceAsStream("type.properties");
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String property = props.getProperty(key);
map.put(key, property);
System.out.println(key + " " + property);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
运行结果如下:
时间: 2024-10-26 20:27:59