.properties文件中的数据是键值对形式的,key = value格式,把此文件放在紧跟在src目录下,新建一个类来读取数据,例如:
public class ReadCommand {
/**
* 读取properties文件
*/
private static ReadCommand readConfig = new ReadCommand();
public static Map<String, String> nodeMap = new HashMap<String, String>();
static{
System.out.println("ReadConfig...");
InputStream in = ReadCommand.class.getClassLoader().getResourceAsStream("light_command.properties");
Properties prop = new Properties();
try {
prop.load(in);
Enumeration en = prop.propertyNames();
while(en.hasMoreElements()){
String key = (String) en.nextElement();
String value = (String) prop.get(key);
nodeMap.put(key, value);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//私有化构造函数
private ReadCommand(){}
/**
* 实例化该类(单例)
* * */
public static ReadCommand getInstance(){
return readConfig;
}
/**
* 获取配置的节点的信息
*
* */
public Map<String, String> getNodes(){
return nodeMap;
}
public static Map<String,String> getLightName(){
Map<String, String> map = ReadConfig.getInstance().getNodes();
return map;
}
Map<String,String> map = GetLightName.getLightName();
Set<String> keys = map.keySet();//得到键值
for(String key : keys){
System.out.println(key+"-----"+map.get(key));
}
}