• java读取和设置配置文件中的属性参数


     想必大家和我一样经常在项目中读取一些配置文件,因为读取的配置很少,结构简单又不想搞个读取xml,比较麻烦。所以这时大多采取读取.properties配置文件,我们和大名鼎鼎的log4j读取配置文件的方式一样。嘿嘿。 

      我自己写了个读取配置文件的java工具类,可以读取某个配置属性或者设置某个属性的值,简单使用。真是java高手必备,开发利器啊!不再掉大家胃口了,直接上代码。哈哈。

    package net.maxt.httpwatch.util;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    
    /**
     * 读取Properties综合类,默认绑定到classpath下的config.properties文件。
     * @author 朱志杰 QQ:695520848
     */
    public class PropertiesUtil {
        //配置文件的路径
        private String configPath=null;
        
        /**
         * 配置文件对象
         */
        private Properties props=null;
        
        /**
         * 默认构造函数,用于sh运行,自动找到classpath下的config.properties。
         */
        public PropertiesUtil() throws IOException{
            InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("config.properties");
            props = new Properties();
            props.load(in);
            //关闭资源
            in.close();
        }
        
        /**
         * 根据key值读取配置的值
         * Jun 26, 2010 9:15:43 PM
         * @author 朱志杰
         * @param key key值
         * @return key 键对应的值 
         * @throws IOException 
         */
        public String readValue(String key) throws IOException {
            return  props.getProperty(key);
        }
        
        /**
         * 读取properties的全部信息
         * Jun 26, 2010 9:21:01 PM
         * @author 朱志杰
         * @throws FileNotFoundException 配置文件没有找到
         * @throws IOException 关闭资源文件,或者加载配置文件错误
         * 
         */
        public Map<String,String> readAllProperties() throws FileNotFoundException,IOException  {
            //保存所有的键值
            Map<String,String> map=new HashMap<String,String>();
            Enumeration en = props.propertyNames();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String Property = props.getProperty(key);
                map.put(key, Property);
            }
            return map;
        }
    
        /**
         * 设置某个key的值,并保存至文件。
         * Jun 26, 2010 9:15:43 PM
         * @author 朱志杰
         * @param key key值
         * @return key 键对应的值 
         * @throws IOException 
         */
        public void setValue(String key,String value) throws IOException {
            Properties prop = new Properties();
            InputStream fis = new FileInputStream(this.configPath);
            // 从输入流中读取属性列表(键和元素对)
            prop.load(fis);
            // 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
            // 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
            OutputStream fos = new FileOutputStream(this.configPath);
            prop.setProperty(key, value);
            // 以适合使用 load 方法加载到 Properties 表中的格式,
            // 将此 Properties 表中的属性列表(键和元素对)写入输出流
            prop.store(fos,"last update");
            //关闭文件
            fis.close();
            fos.close();
        }
        
        public static void main(String[] args) {
            PropertiesUtil p;
            try {
                p = new PropertiesUtil();
                System.out.println(p.readAllProperties());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    代码注释已经将代码解释的很清楚了,再加上代码简单、整洁、高效。(真会给自己贴金)相信大家都已经明白,欢迎大家在项目中使用。也欢迎提建议和拍砖,我已经带上了安全帽了不怕。呵呵。

         差点忘了,程序会自动读取src下的config.properties文件作为配置文件。这点也和log4j一样,真是不错。(再次向大家卖萌)如果大家需要修改配置文件路径,直接在代码中修改就可以了,在构造函数中(一般人我不告诉他)。

    十分感谢:沙琪玛的知识分享

    https://www.cnblogs.com/myzhijie/archive/2013/03/17/2965000.html

    上面是 沙琪玛的知识分享 的原文,写的非常好,直接有效,再次感谢 沙琪玛的知识分享 的无私分享。下面是我在使用中遇到的几点注意。

    一、config.properties 配置文件一定要放在 src 目录下。

    注:不能在 src目录下的其它子目录下。

    PropertiesUtil.java 类文件是可以放在其它子目录下的。

    但要注意 包的引用关系

     

    二、config.properties 示例

    xf_hostUrl=http://111111
    xf_apiKey=22222222222222
    xf_apiSecret=33333333
    xf_appid=4444444444444

    三、config.properties 在IDEA中的创建方法

  • 相关阅读:
    微信证书 javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    【转】ubunt 安装 yum出现 ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法
    HTTP Status 500
    idea导入项目报错:文档中根元素前面的标记必须格式正确
    redis 存取问题
    maven项目怎么引入另一个maven项目
    如何解决failed to push some refs to git
    idea配置maven后提示 commond not found
    JMS规范简介
    java消息中间件的使用与简介
  • 原文地址:https://www.cnblogs.com/hailexuexi/p/16666702.html
Copyright © 2020-2023  润新知