java学习

java学习

 

读取配置文件.properties中的数据

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

posted on 2012-12-11 11:31 杨军威 阅读(265) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜