spring加载properties配置文件

public static void main(String[] args){
 
 String path="E:/workspace/bocMarketData/src/config/PeriodCode.properties";
 try {
Map<String,String> periodCodeMap =readConfigForMap(path);
Set<String> set1 = periodCodeMap.keySet(); 
 for (String s:set1) {
 System.out.println(s+","+periodCodeMap.get(s));
 }

} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 读取文件,生成 Map<String,String>,左边为key 右边为value
* @param path
* @return
* @throws Exception
*/
public static Map<String,String> readConfigForMap(String path) throws Exception{
BufferedReader bf=null;
try{
InputStream in = new FileInputStream(new File(path));
// path  E:/workspace/eDealingV3.0_eTof/src/conf/config/institutionl_IP_Config.properties
Reader reader = new InputStreamReader(in);
  bf=new BufferedReader(reader);
  
}catch(Exception e){
e.getMessage();
}
 
String row=null;
Map<String,String> sMap = new HashMap<String, String>();
while(null!=(row=bf.readLine())){
if(!row.equals("")){
if(row.startsWith("#")){
continue;
}
String key = row.substring(0, row.indexOf("="));
String value = row.substring(row.indexOf("=")+1,row.length());
sMap.put(key, value);
}
}
bf.close();
return sMap;
}

原文地址:https://www.cnblogs.com/riskyer/p/3239091.html