zoukankan      html  css  js  c++  java
  • java读取.properties文件

        在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件。

    案例:

    1:config.properties文件

    name=u843Du82B1u6709u610F
    str=u6D41u6C34u65E0u60C5

    2:PropertiesUtil 类

    package com.gcs.util;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropertiesUtil {
        private static String default_properties = "文件名.properties";
        private static Properties prop;
        static {
            prop = new Properties();
            try {
                InputStream is = new BufferedInputStream(new FileInputStream(getPath() + default_properties));
                prop.load(is);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static String getProperty(String key) {
            return prop.getProperty(key);
        }
    
        public static String getProperty(String key, String defaultValue) {
            String value = prop.getProperty(key);
            if (value == null)
                return defaultValue;
            return value;
        }
    
        public static boolean getBooleanProperty(String name, boolean defaultValue) {
            String value = prop.getProperty(name);
            if (value == null) return defaultValue;
            return (new Boolean(value)).booleanValue();
        }
    
        public static int getIntProperty(String name) {
            return getIntProperty(name, 0);
        }
    
        public static int getIntProperty(String name, int defaultValue) {
            String value = prop.getProperty(name);
            if (value == null) return defaultValue;
            return (new Integer(value)).intValue();
        }
    
        public static String getPath() {
            return Thread.currentThread().getContextClassLoader().getResource("").getPath();
        }
    
        /**
         * 读取指定properties中的值
         * @param properties  文件名
         * @param name  要读取的属性
         * @return
         */
        private String readProper(String properties, String name) {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(properties);
            Properties p = new Properties();
            try {
                p.load(inputStream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return p.getProperty(name);
        }
        
        public static void main(String[] args) {
            PropertiesUtil propertiesUtil = new PropertiesUtil();
            String name = PropertiesUtil.getProperty("name");
            String str = propertiesUtil.readProper("config.properties","str");
            System.out.println("name=="+name+","+"str=="+str);
        }
    }
  • 相关阅读:
    在金融服务计算中,必须要使用BigDecimal
    互联网发展的三个阶段
    k3 cloud服务器端安装顺序
    k3 cloud修改数据库密码后无法连接管理中心
    K3 CLOUD提示库存键重复,提示如下
    k3 cloud总账凭证提示冲突,清除冲突并且第二天开机以后依然提示
    k3 cloud注册插件的时候提示,请选择一个有效的插件程序集
    k3 cloud付款单提示余额不足,科目余额表中余额为正,银行存款流水账中未负数
    k3 cloud提示超出产品激活有效期
    k3 cloud在站点中新增用户
  • 原文地址:https://www.cnblogs.com/sky-/p/5076132.html
Copyright © 2011-2022 走看看