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 编译器 API
Java 密封类:精细化控制继承关系
Java 密封类:精细化控制继承关系
278 83
|
12天前
|
安全 Java 数据建模
Java记录类:简化数据载体的新选择
Java记录类:简化数据载体的新选择
163 101
|
12天前
|
安全 Java 开发者
Java记录类:简化数据载体的新方式
Java记录类:简化数据载体的新方式
181 100
|
2月前
|
安全 IDE Java
Java记录类型(Record):简化数据载体类
Java记录类型(Record):简化数据载体类
299 120
|
17天前
|
存储 小程序 Java
热门小程序源码合集:微信抖音小程序源码支持PHP/Java/uni-app完整项目实践指南
小程序已成为企业获客与开发者创业的重要载体。本文详解PHP、Java、uni-app三大技术栈在电商、工具、服务类小程序中的源码应用,提供从开发到部署的全流程指南,并分享选型避坑与商业化落地策略,助力开发者高效构建稳定可扩展项目。
|
2月前
|
缓存 安全 Java
Java反射机制:动态操作类与对象
Java反射机制是运行时动态操作类与对象的强大工具,支持获取类信息、动态创建实例、调用方法、访问字段等。它在框架开发、依赖注入、动态代理等方面有广泛应用,但也存在性能开销和安全风险。本文详解反射核心API、实战案例及性能优化策略,助你掌握Java动态编程精髓。
|
2月前
|
存储 安全 Java
Java集合框架(一):List接口及其实现类剖析
本文深入解析Java中List集合的实现原理,涵盖ArrayList的动态数组机制、LinkedList的链表结构、Vector与Stack的线程安全性及其不推荐使用的原因,对比了不同实现的性能与适用场景,帮助开发者根据实际需求选择合适的List实现。
|
3月前
|
Java API
深入解析Java API中Object类的功能
了解和合理运用 Object类的这些方法,对于编写可靠和高效的Java应用程序至关重要。它们构成了Java对象行为的基础,影响着对象的创建、识别、表达和并发控制。
65 0
|
3月前
|
安全 Java
JAVA:Collections类的shuffle()方法
`Collections.shuffle()` 是 Java 中用于随机打乱列表顺序的工具方法,适用于洗牌、抽奖等场景。该方法直接修改原列表,支持自定义随机数生成器以实现可重现的打乱顺序。使用时需注意其原地修改特性及非线程安全性。
133 0