Java读取Properties的工具类

  • 作者: 凯哥Java(公众号:凯哥Java)
  • 经验分享
  • 时间:2019-05-27 18:49
  • 2015人已阅读
简介 GetPropertiesValue.javaimport java.util.Properties;public class GetPropertiesValue {   /**    *    * @Title: getVal

🔔🔔好消息!好消息!🔔🔔

 如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号代升级ChatGPT4

有需要的朋友👉:微信号 kaigejava2022

GetPropertiesValue.java

import java.util.Properties;
public class GetPropertiesValue {

   /**
    * 
   * @Title: getValue 
   * @Description: 根据配置文件路径,文件中的KEY获取对应的value
   * @author sizk
   * @param fileNmae     配置文件名称
   * @param key         需要的key
   * @return
   * @return String      返回对应的value
   * @see  
   * @throws
   * @since      创建  2017年2月8日 11:17:41
   * 例如:
   * fileName ftp.properties
   * key  cintractDownImg
    */
   public static String getValue(String fileNmae,String key) {
            Properties prop = UtilsProperties.getPropertyFile(GetPropertiesValue.class, fileNmae);
      return prop.getProperty(key);
   }
}


UtilsProperties.java


import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class UtilsProperties {
   
   /**
    *  根据clazz的物理路径,获取propertyFileName名称的属性文件的Properties对象
    * @param clazz
    * @param propertyFileName
    * @return
    */
   public static Properties getPropertyFile(Class clazz,String propertyFileName){
      InputStreamReader reader= new InputStreamReader(clazz.getClassLoader().getResourceAsStream(propertyFileName));
//      InputStream inputStream = clazz.getClassLoader().getResourceAsStream(propertyFileName);

      Properties p = new Properties();
      try {
//       p.load(inputStream);
         p.load(reader);
      } catch (IOException e) {
         e.printStackTrace();
      }
      return p;
    }

   public static void main(String[] args) {
      System.out.println(""+File.separator);
   }
}

TopTop