.properties文件中的數(shù)據(jù)是鍵值對(duì)形式的,key = value格式,把此文件放在緊跟在src目錄下,新建一個(gè)類(lèi)來(lái)讀取數(shù)據(jù),例如:
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();
}
}
//私有化構(gòu)造函數(shù)
private ReadCommand(){}
/**
* 實(shí)例化該類(lèi)(單例)
* * */
public static ReadCommand getInstance(){
return readConfig;
}
/**
* 獲取配置的節(jié)點(diǎn)的信息
*
* */
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));
}
}