Java.Properties大全

来源:互联网 发布:移动端优化 编辑:程序博客网 时间:2024/04/29 14:23
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法:
一、通过jdk提供的java.util.Properties类。
此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put、putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的。因此java.util.Properties类提供了getProperty()和setProperty()方法来操作属性文件,同时使用store或save(已过时)来保存属性值(把属性值写入.properties配置文件)。在使用之前,还需要加载属性文件,它提供了两个方法:load和loadFromXML。
load有两个方法的重载:load(InputStream inStream)、load(Reader reader),所以,可根据不同的方式来加载属性文件。
可根据不同的方式来获取InputStream,如:

1、通过当前类加载器的getResourceAsStream方法获取


  1. InputStream inStream = TestProperties.class.getClassLoader().getResourceAsStream("test.properties");  


2、从文件获取


  1. InputStream inStream = new FileInputStream(new File("filePath"));  


3、也是通过类加载器来获取,和第一种一样


  1. InputStream in = ClassLoader.getSystemResourceAsStream("filePath");  


4、在servlet中,还可以通过context来获取InputStream

 

  1. InputStream in = context.getResourceAsStream("filePath");  


5、通过URL来获取
[java] view plaincopy
  1. URL url = new URL("path");  
  2. InputStream inStream = url.openStream();  
  1. URL url = new URL("path");  
  2. InputStream inStream = url.openStream();  


读取方法如下:


  1. Properties prop = new Properties();  
  2. prop.load(inStream);  
  3. String key = prop.getProperty("username");  
  4. //String key = (String) prop.get("username");  



二、通过java.util.ResourceBundle类来读取,这种方式比使用Properties要方便一些。
1、通过ResourceBundle.getBundle()静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可。

  1. ResourceBundle resource = ResourceBundle.getBundle("com/mmq/test");//test为属性文件名,放在包com.mmq下,如果是放在src下,直接用test即可  
  2. String key = resource.getString("username");  

2、从InputStream中读取,获取InputStream的方法和上面一样,不再赘述。


  1. ResourceBundle resource = new PropertyResourceBundle(inStream);  


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



1.Properties类与Properties配置文件

  Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

2.Properties中的主要方法

(1)load(InputStream inStream)

   这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象如下面的代码:

Properties pro = new Properties();FileInputStream in = new FileInputStream("a.properties");pro.load(in);in.close();

(2)store(OutputStream out, String comments)

   这个方法将Properties类对象的属性列表保存到输出流中如下面的代码:

FileOutputStream oFile = new FileOutputStream(file, "a.properties");pro.store(oFile, "Comment");oFile.close();

  如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。

  注释信息后面是属性文件的当前保存时间信息。

(3)getProperty/setProperty

   这两个方法是分别是获取和设置属性信息。

3.代码实例

 属性文件a.properties如下:

name=rootpass=liukey=value

读取a.properties属性列表,与生成属性文件b.properties。代码如下:

复制代码
 1 import java.io.BufferedInputStream; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.InputStream;  5 import java.util.Iterator; 6 import java.util.Properties;  7  8 public class PropertyTest { 9     public static void main(String[] args) { 10         Properties prop = new Properties();     11         try{12             //读取属性文件a.properties13             InputStream in = new BufferedInputStream (new FileInputStream("a.properties"));14             prop.load(in);     ///加载属性列表15             Iterator<String> it=prop.stringPropertyNames().iterator();16             while(it.hasNext()){17                 String key=it.next();18                 System.out.println(key+":"+prop.getProperty(key));19             }20             in.close();21             22             ///保存属性到b.properties文件23             FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开24             prop.setProperty("phone", "10086");25             prop.store(oFile, "The New properties file");26             oFile.close();27         }28         catch(Exception e){29             System.out.println(e);30         }31     } 32 }

0 0
原创粉丝点击