您的位置:首页 > 编程语言 > Java开发

spring加载properties配置文件

2013-08-05 19:57 387 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: