PropertiesUtil,读取properties文件

public class PropertiesUtil {

    private static HashMap configMap;

    private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    public static void init(String path) {
        // 把config.properties配置文件中的配置信息放入到Map中
        configMap = new HashMap();
        Properties p = new Properties();
        try {
            //获取要加载的文件
            p.load(new FileInputStream(new File(getClassPath()
                    + path)));
        } catch (IOException e) {
            logger.error("init properties file excetion", e);
        }
        Set keySet = p.keySet();
        for (Object key : keySet) {
            configMap.put(key.toString(), p.getProperty(key.toString()));
        }

    }
      /**
     * 从ClassPath中的读取属性的名称
     * 
     * @param fileName 系统文件名称,在classpath下"#.properties"
     * @param propertyName 属性名称
     * @return
     */
    public static String getPropertys(String fileName, String propertyName) {
        String value = "";
        try {
            Properties props = PropertiesLoaderUtils.loadAllProperties(fileName);
            value = props.getProperty(propertyName);
        } catch (IOException e) {
            logger.debug("file not found ", e);
        }
        return value;
    }
     /**
     * 获取系统文件classpath路径
     * 
     * @return
     */
    public static String getClassPath() {
        return PropertiesUtil.class.getClassLoader().getResource("").getPath().replaceAll("%20", " ");
    }
    /**
     * 设置系统配置文件值
     * 
     * @param fileName
     *            系统文件名称,在classpath下"#.properties"
     * @param map
     *            含有配置文件中属性的键值对儿
     */
    public static boolean setPropertyFile(String fileName,
            Map map) {
        Properties props = new Properties();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(getClassPath() + fileName);
            props.load(in);
            out = new FileOutputStream(getClassPath() + fileName);
            Iterator> iterator = map.entrySet()
                    .iterator();
            while (iterator.hasNext()) {
                Entry entry = iterator.next();
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                props.setProperty(key, value);
            }
            props.store(out,null);
            return true;
        } catch (FileNotFoundException e) {
            logger.debug("file not found ", e);
        } catch (IOException e) {
            logger.debug("can't get stream", e);
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                logger.debug("can't close stream", e);
            }
        }
        return false;
    }

    /**
     * 设置系统配置文件值
     * 
     * @param dir
     *            根据路径找到properties文件
     * @param map
     *            含有配置文件中属性的键值对儿
     */
    public static boolean setPropertyDirFile(String dir, Map map) {
        Properties props = new Properties();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(dir);
            props.load(in);
            out = new FileOutputStream(dir);
            Iterator> iterator = map.entrySet()
                    .iterator();
            while (iterator.hasNext()) {
                Entry entry = iterator.next();
                String key = entry.getKey().toString();
                String value = entry.getValue().toString();
                props.setProperty(key, value);
            }
            props.store(out, null);
            return true;
        } catch (FileNotFoundException e) {
            logger.debug("file not found ", e);
        } catch (IOException e) {
            logger.debug("can't get stream", e);
        } finally {
            try {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                logger.debug("can't close stream", e);
            }
        }
        return false;
    }

    /**
     * 根据key得到配置项的值
     * 
     * @param key
     * @return
     */
    public static String getProperty(String path, String key) {
        init(path);
        return configMap.get(key);
    }

    /**
     * 根据提供po文件 路径和key得到值
     * 
     * @param path
     * @param key
     * @return
     */
    public static String getPropertyByPath(String path, String key) {
        configMap = new HashMap();
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(new File(path)));
        } catch (IOException e) {
            logger.error("init properties file excetion", e);
        }
        Set keySet = p.keySet();
        for (Object key1 : keySet) {
            configMap.put(key1.toString(), p.getProperty(key1.toString()));
        }
        return configMap.get(key);
    }

    /**
     * 根据提供po文件 路径得到键值对
     * 
     * @param path
     * @param key
     * @return
     */
    public static Map getPropertyByPath(String path) {
        configMap = new HashMap();
        Properties p = new Properties();
        try {
            p.load(new FileInputStream(new File(path)));
        } catch (IOException e) {
            logger.error("init properties file excetion", e);
        }
        Set keySet = p.keySet();
        for (Object key1 : keySet) {
            configMap.put(key1.toString(), p.getProperty(key1.toString()));
        }
        return configMap;
    }

} 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(记录)