.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));
}
}