常见的properties文件读取

在实际开发中,对.properties文件的读取操作非常频繁
读取.properties文件的方式主要有两种:
【1 : 通过jdk提供的java.util.Properties类】
java.util.Properties类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put、putAll这 两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的。因此java.util.Properties类提 供了getProperty()和setProperty()方法来操作属性文件,同时使用store或save(已过时)来保存属性值(把属性值写 入.properties配置文件)。在使用之前,还需要加载属性文件,它提供了两个方法:load和loadFromXML。

1.通过当前类加载器的getResourceAsStream方法获取
    InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");  

2.从文件获取
    InputStream inStream = new FileInputStream(new File("filePath"));  

3.也是通过类加载器来获取,和第一种一样
    InputStream in = ClassLoader.getSystemResourceAsStream("filePath");  

4.在servlet中,还可以通过context来获取InputStream
    InputStream in = context.getResourceAsStream("filePath");  

5.通过URL来获取
    URL url = new URL("path");  
    InputStream inStream = url.openStream();  


读取流:
    Properties prop = new Properties();    
    prop.load(inStream);    
    String key = prop.getProperty("username");    
    //String key = (String) prop.get("username");  

【2 : 通过java.util.ResourceBundle类读取】
这种方式比使用Properties要方便一些。可以通过通过ResourceBundle.getBundle()静态方法来获取,也可以直接从InputStream中读取

1.通过ResourceBundle.getBundle()静态方法来获取
    ResourceBundle是一个抽象类,这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。
    //test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可    
    ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");
    String key = resource.getString("username");  

2.从InputStream中读取
     获取InputStream的方法和上面一样,不再赘述。
     ResourceBundle resource = new PropertyResourceBundle(inStream);  

注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定, 如:test.properties入在com.mmq包下,则要使用com/mmq/test.properties(通过Properties来获 取)或com/mmq/test(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用test.properties 或test即可。

【实例】

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.ResourceBundle;

public class TestPro {
    
    public static final String FILE = "system.properties";

    public static void main(String[] args) throws IOException {
        //读取方式一:通过当前类加载器的getResourceAsStream方法获取
        //InputStream inStream = TestPro.class.getClassLoader().getResourceAsStream(FILE); 
        
        //读取方式二:从文件获取
        //InputStream inStream = new FileInputStream(new File("")); 
        
        //读取方式三:过类加载器来获取
        InputStream inStream = ClassLoader.getSystemResourceAsStream(FILE);  
        
        //读取方式四:在servlet中,还可以通过context来获取InputStream
        //InputStream in = context.getResourceAsStream("filePath"); 
        
        //读取方式五:
        //InputStream inStream=new URL("").openStream();
        
        
        Properties prop = new Properties();    
        prop.load(inStream);    
        String key = prop.getProperty("redis.port");
        System.out.println(key);
        
        //方式六:通过java.util.ResourceBundle类读取,这种方式来获取properties属性文件不需要加.properties后缀名
        ResourceBundle resource = ResourceBundle.getBundle("system");
        System.out.println(resource.getString("data.storage.root"));
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,959评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,767评论 18 399
  • 那一年 我们把各种各样的试卷飞舞 那一天 我装作不在乎从你身旁走过 那一刻 脑里是和你同桌三年的点滴 还有 我三年...
    溺茗阅读 162评论 0 0
  • 蒹葭苍苍123阅读 88评论 2 4