美文网首页
常见的properties文件读取

常见的properties文件读取

作者: 先生_吕 | 来源:发表于2017-06-05 16:02 被阅读168次

    在实际开发中,对.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"));
        }
    }
    
    

    相关文章

      网友评论

          本文标题:常见的properties文件读取

          本文链接:https://www.haomeiwen.com/subject/gwnifxtx.html