java.util.Properties类操作properties文件

1 Properties类

由于java.util.Properties类也是属于继承Map的,所以就归类到集合这章了

1.1 认识properties文件

了解并认识properties文件

  1. properties文件是一个文本文件
  2. properties文件的语法有两种,一种是注释,一种属性配置。
    注 释:前面加上#
    属性配置:以键=值的方式书写一个属性的配置信息。
  3. properties文件的一个属性配置信息可以换行,但键不可以换行。值换行用\表示
  4. properties的属性配置键值前后的空格在解析时候会被忽略
  5. properties文件可以只有键而没有值。也可以仅有键和等号而没有值,但无论如何一个属性配置不能没有键

例如,下面一个properties文件

#正确的properties配置文件
aaa=1\
    11
b
bb    =     222
 
#格式良好的properties文件
aaa=111
bbb=222

1.2 了解Properties类

Properties类的层次结构

java.lang.Object
  java.util.Dictionary<K,V>
      java.util.Hashtable<Object,Object>
          java.util.Properties

从层次机构看,Properties类实现了Map接口,因为HashTable实现了Map接口,因此Properties类本质上是一种简单的Map容器。
实际上,Properties类本身表示了对一种Map结构的操作。properties文件本身就表示了一个键值对的集合。因此,Properties类属于集合容器的家族,在使用前应该创建一个Properties的容器,实际上就是创建一个默认不带参数的 Properties对象。以后通过别的方式给里面添加键值对

1.3 properties文件与Properties类的关系

通过properties文件可以填充Properties
也可以通过xml文件来填充Properties类。
可以通过绝对路径方式加载Properties文件信息,也可以使用相对路径加载

1.4 使用Properties类

1.4.1 操作properties

public static void test1() throws IOException {	
        InputStream is = PropertiesDemo.class.getResourceAsStream("/log4j.properties");
        
        Properties p = new Properties();
        p.load(is);
        for (Object key : p.keySet()) {
            System.out.println(key + "=" + p.get(key));
        }
    }
    public static void main(String[] args) throws Exception {
        test1();
  }

1.4.2 操作XML文件

public static void testProperties() throws IOException {

        //将properties文件加载到输入字节流中
        InputStream is = new ClassPathResource("logback.xml").getInputStream();
        //创建一个Properties容器
        Properties prop = new Properties();
        //从流中加载properties文件信息
        prop.load(is);
        //循环输出配置信息
        for (Object key : prop.keySet()) {
            System.out.println(key + "=" + prop.get(key));
        }

        //定义一个输出流
        OutputStream os1 = new FileOutputStream("ttt.xml");
        OutputStream os2 = new FileOutputStream("ttt.properties");

        //从Properties对象导出导出到xml
        prop.storeToXML(os1, "我从properties导出的XML配置文件");
        //从Properties对象导出properties文件
        prop.store(os2, "我从properties导出的XML配置文件");

        is.close();
        os1.close();
        os2.close();
    }
 public static void main(String[] args) throws Exception {
        testProperties();
}

1.4.3 java读取properties文件的九种方法

点击了解 从jar包中读取资源文件(原理深究)
假如读取的是log4j.properties
在这里插入图片描述
java读取properties文件的九种方法:

  1. 使用java.util.properties类的load()方法
    示例:
InputStream in = new BufferedInputStream(new FileInputStream("绝对路径"))
properties p = new properties()
p.load(in)
  1. 使用class变量的getResourceAsStream()方法
    示例:
    注意:此种写法是采用了绝对路径/
Inputstream in = PropertiesDemo.class.getResourceAsStream("/log4j.properties")
properties p = new properties()
p.load(in)
  1. 使用class.getClassLoader()所得到的java.lang.ClassLoadergetResourceAsStream()方法
    注意:Linux中无法直接访问未经解压的文件,会找不到文件,只能使用流的方式对静态资源进行读取
    示例:
Inputstream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("log4j.properties")
properties p = new properties()
p.load(in)
  1. 使用当前类加载器Thread.currentThread().getContextClassLoader()getResourceAsStream方法
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.properties");
properties p = new properties()
p.load(in)
  1. 使用java.lang.ClassLoader类的getsystemresourceasstream()静态方法
    示例:
inputstream in = ClassLoader.getSystemResourceAsStream("log4j.properties")
properties p = new properties()
p.load(in)
  1. 使用ClassPathResource的构造方法加载读取
    注意:可以在linux环境读取资源但是不能访问未解压的压缩文件
ClassPathResource classPathResource = new ClassPathResource("log4j.properties");
InputStream is = classPathResource.getInputStream();
  1. 使用spring自带的ResourceUtils加载
    注意:只有开发环境(IDE中)时可以读取到,生产环境(linux下jar包)读取失败
File file = ResourceUtils.getFile("classpath:log4j.properties");
InputStream is = new FileInputStream(file);
  1. 使用java.util.ResourceBundle类的getBundle()方法,只用输入配置文件名字不用加后缀
    示例:
ResourceBundle rb = ResourceBundle.getBundle("log4j",Locale.getdefault())
Enumeration<String> keys = rb.getKeys();
        while (keys.hasMoreElements()){
            String s = keys.nextElement();
            System.out.println(s);
        }
  1. 使用java.util.PropertyResourceBundle类的构造函数
    示例:
Inputstream in = new Bufferedinputstream(new fileinputstream("绝对路径"))
ResourceBundle rb = new PropertyResourceBundle(in)

附录:点击了解SpringBoot中yml文件读取方式

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值