Java源码类 - Properties类及多种读取方式

简介: Java源码类 - Properties类及多种读取方式

Java源码类 - Properties类

一、Properties概述

Properties是JDK1.0中引入的java类,在项目中大量使用。表示一个持久的属性集,可以保证在流中或从流中加载,属性列表中每个键及其值都是一个字符串

主要作用:通过修改配置文件可以方便的修改代码中的参数,实现不用改class文件即可灵活变更参数。

二、Properties类图

java.util.Properties继承自java.util.Hashtable,是一个持久化的属性保存对象,可以将属性内容写出到可以将属性内容写出到stream中或者从stream中读取属性内容。

三、Properties重要特性

  1. 在底层的Hashtable中,每一对属性的key和value都是按照string类型来保存的;
  2. Properties支持文本方式和xml方式的数据存储。在文本方式中,格式为key:value,其中分隔符可以是:冒号(😃、等号(=)、空格。其中空格可以作为key的结束,同时获取的值回将分割符号两端的空格去掉;
  3. Properties可以将其他的Properties对象作为默认的值;
  4. Hashtable的所有方法Properties对象均可以访问,但是不建议这么做,因为Hashtable可以存放其他数据类型,这样会导致Properties一些方法调用报错;
  5. 在properties文件中,可以用井号"#"来作注释;
  6. 线程安全;
  7. key、value不可以是Null;

四、Properties主要的方法

方法名 方法说明 备注
setProperty(String key,String value) 向Properties对象中添加参数和值 -
getProperty(String key) 获取指定参数值
load(InputStream in) 以字节输入流加载配置文件到对象中
load(Reader in) 以字符输入流加载配置文件到对象中
store(OutputStream out,String comments) 以字节输出流将对象中的内容输出到配置文件中
store(Writer out, String comments) 以字符输出流将对象中的内容输出到配置文件中

五、Properties六种读取方法

5.1 使用java.util.Properties类的load()方法
public  void runProperties(){
    try {
          String path_properties=  this.getClass().getClassLoader().getResource("customer.properties").getPath();
          InputStream inputStream = new BufferedInputStream(new FileInputStream(path_properties));
          Properties p = new Properties();
          p.load(inputStream);
          System.out.println(p.getProperty("goyeer.name"));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
5.2 使用java.util.ResourceBundle类的getBundle()方法
public void runResourceBundle(){
        try {
            Locale locale = new Locale("zh", "CN");
            ResourceBundle resource = ResourceBundle.getBundle("customer",locale);
            String gy_name = resource.getString("goyeer.name");
            System.out.println(gy_name);
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
 }
5.3 使用java.util.PropertyResourceBundle类的构造函数
public  void runPropertyResourceBundle(){
        try {
            String path_properties=  this.getClass().getClassLoader().getResource("customer.properties").getPath();
            InputStream inStream = new BufferedInputStream(new FileInputStream(path_properties));
            ResourceBundle rb = new PropertyResourceBundle(inStream);
            String gy_name = rb.getString("goyeer.name");
            System.out.println(gy_name);
        }catch (Exception exception){
            System.out.println(exception.getMessage());
        }
    }
5.4 使用class变量的getResourceAsStream()方法
public void runJProperties(){
       try
       {
           InputStream in = this.getClass().getResourceAsStream("/customer.properties");
           Properties p = new Properties();
           p.load(in);
           String gy_name = p.getProperty("goyeer.name");
           System.out.println(gy_name);
       }catch (Exception exception){
           System.out.println(exception.getMessage());
       }
    }
5.5 使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
public void runClassLoaderAndStream(){
        try {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream("customer.properties");
            Properties p = new Properties();
            p.load(in);
            String gy_name = p.getProperty("goyeer.name");
            System.out.println(gy_name);
        }catch (Exception exception){
            System.out.println(exception.getMessage());
        }
    }
5.6 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
public void runClassLoaderAndSystemResourceAsStream() {
        try {
            InputStream in = ClassLoader.getSystemResourceAsStream("customer.properties");
            Properties p = new Properties();
            p.load(in);
            String gy_name = p.getProperty("goyeer.name");
            System.out.println(gy_name);
        } catch (Exception exception) {
            System.out.println(exception.getMessage());
        }
    }
5.6 在Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
public void runServletContext(ServletContext context){
        try {
            String path=  this.getClass().getClassLoader().getResource("customer.properties").getPath();
            InputStream in = context.getResourceAsStream(path);
            Properties p = new Properties();
            p.load(in);
            String gy_name = p.getProperty("goyeer.name");
            System.out.println(gy_name);
        }catch (Exception exception){
            System.out.println(exception.getMessage());
        }
    }

对应properties文件操作的方法比较多,根据不同的场景选择适合的方法。


目录
相关文章
|
3月前
|
安全 Java 数据建模
Java记录类:简化数据载体的新选择
Java记录类:简化数据载体的新选择
251 101
|
3月前
|
安全 Java 开发者
Java记录类:简化数据载体的新方式
Java记录类:简化数据载体的新方式
295 100
|
2月前
|
存储 Java 索引
用Java语言实现一个自定义的ArrayList类
自定义MyArrayList类模拟Java ArrayList核心功能,支持泛型、动态扩容(1.5倍)、增删改查及越界检查,底层用Object数组实现,适合学习动态数组原理。
110 4
|
2月前
|
IDE JavaScript Java
在Java 11中,如何处理被弃用的类或接口?
在Java 11中,如何处理被弃用的类或接口?
193 5
|
2月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
178 1
|
2月前
|
Java Go 开发工具
【Java】(8)正则表达式的使用与常用类分享
正则表达式定义了字符串的模式。正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。
235 1
|
2月前
|
存储 Java 程序员
【Java】(6)全方面带你了解Java里的日期与时间内容,介绍 Calendar、GregorianCalendar、Date类
java.util 包提供了 Date 类来封装当前的日期和时间。Date 类提供两个构造函数来实例化 Date 对象。第一个构造函数使用当前日期和时间来初始化对象。Date( )第二个构造函数接收一个参数,该参数是从1970年1月1日起的毫秒数。
186 1
|
2月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
205 1
|
2月前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
308 5
|
4月前
|
缓存 安全 Java
Java反射机制:动态操作类与对象
Java反射机制是运行时动态操作类与对象的强大工具,支持获取类信息、动态创建实例、调用方法、访问字段等。它在框架开发、依赖注入、动态代理等方面有广泛应用,但也存在性能开销和安全风险。本文详解反射核心API、实战案例及性能优化策略,助你掌握Java动态编程精髓。