zoukankan      html  css  js  c++  java
  • 如题,properties配置文件在项目中是经常用到的,那么读取properties配置文件的方法有哪些呢?


    方法一:可以通过java.util.Properties类的load()方法

    1 InputStreamin=lnewBufferedInputStream(newFileInputStream(name));
    2 Propertiesp=newProperties();
    3 p.load(in);

    方法二:利用spring来读取properties配置文件
    org.springframework.beans.factory.support.PropertiesBeanDefinitionReader


    方法三:通过java.util.ResourceBundle类的getBundle()方法

    1 ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

    方法四:通过java.util.PropertyResourceBundle类的构造函数

    1 InputStream in = new BufferedInputStream(new FileInputStream(name));
    2 ResourceBundle rb = new PropertyResourceBundle(in);

    感谢@luochen1314 提供的一种方法
    版本二:

    ResourceBundle res = ResourceBundle.getBundle(baseName);
    baseName为 包路径/文件名(不带后缀)
    比如properties文件在com.tt包下,文件名为tt.properties
    则baseNamewei com/tt/tt


    方法五:使用class变量的getResourceAsStream()方法

    1 InputStream in = JProperties.class.getResourceAsStream(name);
    2 Properties p = new Properties();
    3 p.load(in);

    方法六:通过class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法

    1 InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
    2 Properties p = new Properties();
    3 p.load(in);

    方法七:通过java.lang.ClassLoader类的getSystemResourceAsStream()静态方法

    1 InputStream in = ClassLoader.getSystemResourceAsStream(name);
    2 Properties p = new Properties();
    3 p.load(in);

    方法八:Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法

    1 InputStream in = context.getResourceAsStream(path);
    2 Properties p = new Properties();
    3 p.load(in);
  • 相关阅读:
    plink:将bed文件转化为ped,map文件
    linux下查找某文件关键字(grep 函数)
    genetic model
    linux下设置默认路径
    vi怎么查找关键字
    有意思的undefined columns selected,源于read.table和read.csv
    练习2-2
    练习2-1
    排序算法之堆排序
    Java实现二叉树先序,中序,后序,层次遍历
  • 原文地址:https://www.cnblogs.com/zhaoyfk/p/6306317.html
Copyright © 2011-2022 走看看