PropertiesUtil、propertiesファイルを読み込む

11170 ワード

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

}