1 properties简介:

properties是一种文本文件,内容格式为:
     key = value     #单行注释
适合作为简单配置文件使用,通常作为参数配置、国际化资源文件使用。
对于复杂的配置,就需要使用XML、YML、JSON等了

2 java加载Properties:

java加载properties主要通过2个util包下的工具类: Properties类、 ResourceBundle类

2.1 通过Properties类加载:

Properties类通过load()方法加载配置信息,这个方法接收一个输入流参数:InputStream、Reader。
     Properties提供get(String key) 方法读取指定的配置项。

2.1.1 通过ClassLoader获取InputStream加载:

  该方式只能读取类路径下的配置文件

(1)先创建Properties对象

(2)获取property文件对应的输入流in

(3)使用Properties加载输入流in

(4)通过Properties.get(key)方法获取配置,如果配置信息不存在,则返回null

/**
  * 基于ClassLoader读取properties;
  * 该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。
  */
public static void method1() {
     System.out.println("使用ClassLoader方式加载properties");
     Properties properties = new Properties();
     /*
      * 使用ClassLoader加载properties配置文件生成对应的输入流。
      * 使用此种方式,要求property文件必须要放在src目录下,编译之后会被放到class文件相同目录下。
      * 因为ClassLoad的基础路径是相对于编译后class文件所在目录(可能是bin,classes),如果将properties放在项目根目录下,使用此种方式可能会找不到 properties文件
      */
     //必须要以 /开始    , 是在classes文件根目录下寻找
     InputStream in = PropertiesDemo.class.getResourceAsStream("/properties/a.properties");
     System.out.println(PropertiesDemo.class.getClassLoader().getResource(".").getPath());
     try {
        properties.load(in);
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }finally {
         closeResource(in);
     }
    
     System.out.println(properties.get("name"));
     System.out.println(properties.get("age"));
}

2.1.2 通过构建Reader加载:

该方式的优点在于可以读取任意路径下的配置文件

(1)创建Properties对象

(2)创建一个Reader,可以指定路径,不局限于类路径

(3)使用Properties加载Reader

(4)Properties.get(key)方法读取配置

/**
  * 使用inputStream读取配置文件
  * 该方式的优点在于可以读取任意路径下的配置文件
  */
public static void method2() {
     System.out.println("使用InputStream方式加载properties");
     Properties properties = new Properties();
      BufferedReader reader = null;
      try {
          /*
           * System.getProperty("user.dir"): 获取项目根路径,  而后附加配置文件的路径
          */
          reader = new BufferedReader(new FileReader(System.getProperty("user.dir") + "/properties/a.properties"));
          properties.load(reader);
          System.out.println(properties.get("name"));
          System.out.println(properties.get("age"));
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }finally {
          closeResource(reader);
      }
  }

2.2 通过ResourceBundle类加载:

ResourceBundle读取配置有2种方式:

(1)指定文件路径 :相对于src、classes的相对路径
    (2)提供InputStream
      ResourceBundle提供方法getString(key) 和 getObject(key)读取配置项
     使用路径加载,不需要指定文件后缀名且不需要手动关闭相关资源,比Properties类操作要简单

/**
  * 通过ResourceBundle 读取配置, 此种方式项目Properties要简单
  * ResourceBundle读取配置有2种方式: (1)指定文件路径 (2)提供InputStream
  */
public static void method3() {
     System.out.println("使用ResourceBundle方式加载properties");
     /*
      * (1)直接指定文件路径:

*   可以使用相对路径,从类路径开始,且不需要指定properties文件的后缀
      */
     ResourceBundle resource = ResourceBundle.getBundle("properties/b");
     System.out.println(resource.getString("name"));
     System.out.println(resource.getString("age"));

try {
         /*
          * (2)通过InputStream加载配置:

*    通过当前类的class实例,获取资源输入流
          */
         resource = new PropertyResourceBundle(PropertiesDemo.class.getResourceAsStream("/properties/a.properties"));
         System.out.println(resource.getString("name"));
         System.out.println(resource.getString("age"));
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
}

java加载property文件配置的更多相关文章

  1. Java加载资源文件的两种方法

    处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...

  2. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  3. java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...

  4. java加载外部文件数据到代码中:外部数据文件放到jar包中,调用方法getResourceAsStream

    任务要将数据文件geo.txt加载进行.因为是别人写的总体项目,不能乱动位置.只能将geo.txt打包到jar中某目录.比如,放到.class文件下怎么加载:http://riddickbryant. ...

  5. Java加载jar文件并调用jar文件当中有参数和返回值的方法

    在工作当中经常遇到反编译后的jar文件,并要传入参数了解其中的某些方法的输出,想到Java里面的反射可以实现加载jar文件并调用其中的方法来达到自己的目的.就写了个Demo代码. 以下的类可以编译生成 ...

  6. Django模版中加载静态文件配置详解

    .settings.INSTALLED_APPS下添加:django.contrib.staticfiles .settings.py下添加:STATIC_URL = '/static/' . ()在 ...

  7. Java加载Class文件的原理机制

    详见:http://blog.sina.com.cn/s/blog_6cbfd2170100ljmp.html 1.Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器 ...

  8. java加载资源文件

    className.class.getResourceAsStream 用法: 第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件c ...

  9. Java加载资源文件几种方法

    from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework. ...

随机推荐

  1. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Mac OS/Windows好用软件分享

    下软件全部为破解版,仅供参考学习用,如涉及商业. 请支持正版!谢谢 全部为本人亲测过 看上哪个留言发给你!   直接全分享上来会有人居心不良!

  3. 开源PLM软件Aras详解八 Aras之RelationshipTypes关系类详解

    在Aras中,在之前ItemType解析中有提到,Aras中实际ItemType对应的就是一张表,那么,ItemType与ItemType之间是如何关联的呢, 如果我们需要捋清楚ItemType与It ...

  4. JNI传递修改自定义Java Class数组数据

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 结合前面讲的2篇关于JNI的文章,这里直接把代码贴上,主要是要知道如果传递自定义Class Array的时 ...

  5. HashMap的fast-fail和ConcurrentHashMap的fail-safe实例

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 Java HashMap遍历过程中如果元素被修改会导致遍历失败,ConcurrentHashMap则不会有 ...

  6. idea 使用技巧 - [copy reference]

    选择项目中某个函数名, 右键可以看到 copy reference, 点击完成复制symbol, 分享给别人. shift + shift 打开打开一个对话框, 把分享的symbol粘贴上去, 可以跳 ...

  7. 批量导出数据库表(oracle)

    批量导出数据库表(oracle) 要求:导出sql文件,包含表结构和数据. 方案一 1:用cmd进入命令行输入:tnsping cmstar就是测试172.18.13.200是否连接成功2:导入与导出 ...

  8. PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  9. (递归)P1036 选数

    #include<stdio.h>#include<math.h>int x[20],n,k,i; //判断是否质数 int isprime(int n){    for(i= ...

  10. ZJNU 2349 - 抽抽抽

    为4的倍数,列出所有可能情况再判断即可 处理输入的数据对4取模 可得 4444 2244 2222 1111 3333 1133 1223 1344 1124 3324 共十种情况 所以得出答案 #i ...