比较简单的 读属性文件中的内容

来源:互联网 发布:绝地求生优化 编辑:程序博客网 时间:2024/06/08 19:03

读取属性文件的主方法

    /**      * 通过java.util.resourceBundle来解析properties文件。      *       * @param String  path:properties文件的路径      * @param String   key: 获取对应key的属性      * @return String:返回对应key的属性,失败时候为空。      */      public static String getPropertyByName(String path, String key) {          String result = null;          try {              result = ResourceBundle.getBundle(path).getString(key).trim();          } catch (Exception e) {              e.printStackTrace();          }          return result;      }  // 对于String path的填写,要注意。一般分为两种情况:      /*      * 1、.properties文件在src目录下面,文件结构如下所示:      * src/test.properties      *       * 2、.properties文件在src目录下面的一个包中,因为可能我们经常习惯把各种properties文件建立在一个包中。文件结构如下:      * src/config/test1.properties      */      public static void main(String[] args) {                  // 对于第一种情况,在main函数中使用方法如下:          System.out.println(GetConfigureInfo.getPropertyByName("test", "key1"));                    // 对于第二种情况,在main函数中使用方法如下:          System.out.println(GetConfigureInfo.getPropertyByName("config.test1", "key1"));      }