读取.properties配置文件的内容至Map中

时间:2024-10-30 10:15:14
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • /**
  • * 读取.properties配置文件的内容至Map中
  • * @author libo
  • *
  • */
  • public class PropertiesUtil {
  • /**
  • * 读取.properties配置文件的内容至Map
  • * @param propertiesFile
  • * @return
  • * @throws IOException
  • */
  • public static Map read(String propertiesFile) {
  • try {
  • // PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  • //
  • // Resource resouces[] = (propertiesFile);
  • if (!(("properties") >0))
  • {
  • propertiesFile = propertiesFile + ".properties";
  • }
  • InputStream inStream =().getContextClassLoader().getResourceAsStream(propertiesFile);
  • Properties p = new Properties();
  • (inStream);
  • Map<Object,Object> map = properties2map(p);
  • return map;
  • } catch (IOException e) {
  • throw new Exception(e);
  • }
  • }
  • /**
  • * 将属性文件转为map
  • * @param prop
  • * @return
  • */
  • public static Map properties2map(Properties prop){
  • Map<Object,Object> map = new HashMap<Object,Object>();
  • Enumeration enu = ();
  • while (()) {
  • Object obj = ();
  • Object objv = prop.get(obj);
  • (obj, objv);
  • }
  • return map;
  • }
  • }