星空网 > 软件开发 > Java

java读取.properties文件

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

案例:

1:config.properties文件

name=\u843D\u82B1\u6709\u610Fstr=\u6D41\u6C34\u65E0\u60C5

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

 




原标题:java读取.properties文件

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

国外邮政小包:https://www.goluckyvip.com/tag/102566.html
邮政小包国外:https://www.goluckyvip.com/tag/102567.html
国际ems包裹:https://www.goluckyvip.com/tag/102569.html
美国小包价格:https://www.goluckyvip.com/tag/102571.html
寄国外包裹多少钱:https://www.goluckyvip.com/tag/102572.html
国际大牌背包:https://www.goluckyvip.com/tag/102573.html
怎样做出一个有利可图的SaaS产品?:https://www.kjdsnews.com/a/1836639.html
【再放信号】美国Etsy即将放开中国卖家入驻,官方邮件你收到了吗?:https://www.kjdsnews.com/a/1836640.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流