properties文件读取进map并取值

type.properties

left=factorymethod.LeftHair
right=factorymethod.RightHair
in=factorymethod.InHair


import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
//读取properties内容并转换成map形式
public class PropertiesReader {
   public Map getProperties(){
       Properties props = new Properties();
       Map map=new HashMap();
       try {
        InputStream in= getClass().getResourceAsStream("Type.properties");
        props.load(in);
        Enumeration en=props.propertyNames();
        while (en.hasMoreElements()) {
            String key=(String) en.nextElement();
            String property=props.getProperty(key);
            map.put(key, property);
            System.out.println(key + "."+property);
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
       return map;
   }
}



public HairInterface getHairByClassKey(String key){
        try {
            Map map=new PropertiesReader().//
                    getProperties();
            HairInterface hair= (HairInterface) //
                    Class.forName(map.get(key)).newInstance();
            return hair;
        } catch (InstantiationException e) {
            e.printStackTrace();
        }catch (IllegalAccessException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(开发遇到的问题)