zoukankan      html  css  js  c++  java
  • PropertiesHelper

    package cn.net.comsys.update.util;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.URL;
    import java.net.URLDecoder;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;

    import javax.imageio.stream.FileImageInputStream;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    /**
     * 
    @author JiF
     * 
    @version 创建时间:2011-9-28 下午08:33:52
     * E-Mail:liaojifeng@163.com
     * 类说明:
     *
     *
     *
     
    */
    public class PropertiesHelper {
        private static Log log = LogFactory.getLog(PropertiesHelper.class);
        private  InputStream inputStream = null;
        private  Properties props=null;
        //初始化
        public  PropertiesHelper(){
               try {
                inputStream = new FileInputStream(PropertiesHelper.getProjectPath()+"configure/config.properties");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                log.error("升级程序配置文件找不到."+e.getMessage());
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
               props = new Properties();
               try {   
                   props.load(inputStream);     
               } catch (IOException e1) {   
                e1.printStackTrace();   
               }finally{}

        }
        
        /**
         * 获取某个属性
         
    */
        public String getProperty(String key){
            return props.getProperty(key);
        }
        /**
         * 获取所有属性,返回一个map,不常用
         * 可以试试props.putAll(t)
         
    */
        public Map getAllProperty(){
            Map map=new HashMap();
            Enumeration enu = props.propertyNames();
            while (enu.hasMoreElements()) {
                String key = (String) enu.nextElement();
                String value = props.getProperty(key);
                map.put(key, value);
            }
            return map;
        }
        /**
         * 在控制台上打印出所有属性,调试时用。
         
    */
        public void printProperties(){
            props.list(System.out);
        }
     
        
        /**
         * 关闭配置文件
         * 
    @author JiF
         * 
    @version 创建时间:2011-9-28 下午08:38:31
         * E-Mail:liaojifeng@163.com
         * 说明
         *
         *
         
    */
        public  void closePropertiesFile(){

               if (inputStream!=null){
                   try {
                    inputStream.close();
                } catch (IOException e) {

                }
               }
           
        }
        
        
        /**
         * 获取工程所在目录
         * 
    @return
         * 
    @throws UnsupportedEncodingException
         
    */
        private static String getProjectPath() throws UnsupportedEncodingException{
            URL url = PropertiesHelper.class.getProtectionDomain().getCodeSource().getLocation();
            String filePath = URLDecoder.decode(url.getPath(), "UTF-8");
            if(filePath.endsWith(".jar"))
                filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
            return filePath;
        }
    }
  • 相关阅读:
    新版本ADT创建Android项目无法自动生成R文件解决办法
    关联android-support-v4源码关联不上的解决办法
    关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法
    如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
    ProgressBar+WebView实现自定义浏览器
    Android之ProgressBar读取文件进度解析
    Android开发之ListView添加多种布局效果演示
    ubuntu释放snapd旧文件
    rte_kni
    follow RISC-V
  • 原文地址:https://www.cnblogs.com/jifeng/p/2194856.html
Copyright © 2011-2022 走看看