zoukankan      html  css  js  c++  java
  • properties配置文件

    java.util.Properties基础
    读取:
    Properties prop = new Properties();//属性集合对象    
    FileInputStream fis = new FileInputStream("prop.properties");//属性文件流    
    prop.load(fis);//将属性文件流装载到Properties对象中
    prop.getProperty("key");   //根据指定key获取properties中对应的值
    prop.getProperty("key","value");   //根据指定key获取properties中对应的值,如果这个key未定义,则返回默认值vlaue

    写入

    Properties prop = new Properties();//属性集合对象 
    prop.setProperty("key1", "value1"); //修改key1的属性值 
    prop.setProperty("key2", "value2"); //添加一个新的属性key2
    //文件输出流 
    FileOutputStream fos = new FileOutputStream("prop.properties"); 
    //将Properties集合保存到流中 
    prop.store(fos, "remark"); 
    fos.close();//关闭流 

    properties配置文件util类

        private String fileName = null;
        private String propKey = null;
        private URL url = null;
        Properties properties = new Properties();
        
        /**
         * 根据url实例化properties
         * @param url
         * @throws URISyntaxException
         * @throws IOException
         */
        public PropUtil(URL url) throws URISyntaxException, IOException {
            this.url = url;
            File file =new File(url.toURI());
            if (file!=null) {
                FileInputStream fis = new FileInputStream(file);
                properties.load(fis);
            }
        }
    
        /**
         * 根据文件全路径实例化properties
         * @param fileName
         */
        public PropUtil(String fileName) {
            this.fileName = fileName;
            try {
                if (fileName!=null&&!fileName.isEmpty()) {
                    InputStream is = new FileInputStream(fileName);
                    properties.load(is);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /**
         * 根据properties的key值获取value
         * @param key
         * @return
         */
        public String getPro(String key) {
            String value = properties.getProperty(key);    
            return value;
        }

    源码

  • 相关阅读:
    (转)正则表达式与Python(RE)模块
    (转)【面试】【MySQL常见问题总结】【03】
    (转)MySQL性能调优my.cnf详解
    (转)python logging模块
    (转)python collections模块详解
    mysql故障总结
    rocksdb 编译安装 日志
    c++11 gcc4.8.x安装
    Install ssdb-rocks on CentOS 6
    在Shell里面判断字符串是否为空
  • 原文地址:https://www.cnblogs.com/aeolian/p/properties.html
Copyright © 2011-2022 走看看