Chinaunix首页 | 论坛 | 博客
  • 博客访问: 251167
  • 博文数量: 45
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 488
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-13 14:43
文章分类

全部博文(45)

文章存档

2014年(2)

2013年(35)

2012年(8)

我的朋友

分类: Java

2014-01-17 12:46:22


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;




@Service
public class ResourceMap {

private static Map resourceMap = init();
private static Logger log =Logger.getLogger( ResourceMap.class.getName() );


private static Map init() {
// TODO Auto-generated method stub
Map  initMap = new ConcurrentHashMap();
List fileList = getAllPropertiesFile("TestProperties");
for(File f : fileList){
Properties prop = getProperties(f);
propTransIntoMap(initMap,prop);
}
return initMap;
}


public String get(String key){
return resourceMap.get(key);
}

private static void propTransIntoMap(Map initMap,
Properties prop) {
// TODO Auto-generated method stub
Set keySet = prop.keySet();
for(Object key : keySet){
String propKey = ((String) key).trim();
String propValue = prop.getProperty((String) key).trim();
initMap.put(propKey, propValue);
}
}


private static Properties getProperties(File file) {
// TODO Auto-generated method stub
Properties properties = new Properties();
try {
properties.load(new FileInputStream(file));
} catch (FileNotFoundException e) {
log.error("properties file not found");
e.printStackTrace();
} catch (IOException e) {
log.error("properties file IO exception");
e.printStackTrace();
}
return properties;
}


private static List getAllPropertiesFile(String dir) {
// TODO Auto-generated method stub
return getAllPropertiesFile(dir, ".properties");
}


private static List getAllPropertiesFile(String dir, String endStr) {
// TODO Auto-generated method stub
List list = new ArrayList();
File file = new File(ResourceMap.class.getClassLoader()
.getResource(dir).getPath());
if (file.isDirectory()) {
for (File f : file.listFiles()) {
System.out.println("......Name:"+f.getName()+"......");
if (f.getName().endsWith(endStr)) {
list.add(f);
}
}
}
return list;
}


}

阅读(841) | 评论(0) | 转发(0) |
0

上一篇:spring task定时任务

下一篇:没有了

给主人留下些什么吧!~~