java.util.properties使用方法

http://www.cnblogs.com/lingiu/p/3468464.html

方便好使的java.util.Properties类

今天偶然碰到这个类,发现jdk中这些平时不大用到的类还挺好玩儿的,用起来也特别实在方便,随便写点记录下。

java.util.Properties是对properties这类配置文件的映射。支持key-value类型和xml类型两种。

key-value类型的配置文件大略长这样:

复制代码
#测试环境配置:平台路径配置

jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/

dbPort = localhost

databaseName = myd

dbUserName = root
复制代码

#打头的是注释行,Properties会忽略注释。允许只有key没有value。

例如这样:

复制代码
#测试环境配置:平台路径配置

jstrd_home=D:/TMS2006/webapp/tms2006/WEB-INF/

dbPort = 
databaseName
复制代码

这种情况下,value会被set成null。

properties类实现了Map接口,所以很明显,他是用map来存储key-value数据,所以也注定存入数据是无序的,这个点需要注意。只能通过key的方式来get对应value。

针对key-value这种配置文件,是用load方法就能直接映射成map,非常简单好用。这种配置文件也是我们最重要碰到的配置文件,利用properties读取这类文件到内存一行代码就欧科,比自己解析强大多了,这点很赞。

读取配置文件的大略代码如下:

复制代码
 1 public class LoadSample {  
 2     public static void main(String args[]) throws Exception {  
 3       Properties prop = new Properties();  
 4       FileInputStream fis =   
 5         new FileInputStream("sample.properties");  
 6       prop.load(fis);  
 7       prop.list(System.out);  
 8       System.out.println("\nThe foo property: " +  
 9           prop.getProperty("foo"));  
10     }  
11 }  
复制代码

第六行的load方法直接生产一个内存map,第九行就能get到对应的value了,简单快捷。

这里的第七行list方法是一个输出方法,这边是输出到console,也可以输出到文件等,就能实现内存写入配置文件了。

比如这样:

复制代码
 1 //通过list 方法将Properties写入Properties文件
 2 import java.io.IOException;
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.PrintStream;
 6 import java.util.Properties;
 7 
 8 public class Test {
 9     public static void main(String[] args) {
10 
11         Properties p = new Properties();
12         p.setProperty("id","dean");
13         p.setProperty("password","123456");
14 
15         try{
16             PrintStream fW = new PrintStream(new File("e:\\test1.properties"));
17           p.list(fW );} catch (IOException e) {
18           e.printStackTrace();
19 
20         }
21     }
22 }        
复制代码

这样就能把内存中的properties对象写入到文件中了。

 

另外一种配置形式是xml形式的,这种配置相对上面一种就少见一点。

 

xml形式的配置文件格式大略是这样:

复制代码
<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
<comment>Hi</comment>  
<entry key="foo">bar</entry>  
<entry key="fu">baz</entry>  
</properties>  
复制代码

读取xml配置跟读取kv配置没差别,就是把load换成xml对应的loadFromXML方法,代码大略是这样:

复制代码
 1 public class LoadSampleXML {  
 2     public static void main(String args[]) throws Exception {  
 3       Properties prop = new Properties();  
 4       FileInputStream fis =  
 5         new FileInputStream("sampleprops.xml");  
 6       prop.loadFromXML(fis);  
 7       prop.list(System.out);  
 8       System.out.println("\nThe foo property: " +  
 9           prop.getProperty("foo"));  
10     }  
11 }  
复制代码

把内存中的properties对象写入到xml文件中也和上面差不多,就是把list方法改成xml对应的storeToXML方法。

代码大略是这样:

复制代码
 1 import java.io.IOException;
 2 import java.io.File;
 3 import java.io.FileInputStream;
 4 import java.io.PrintStream;
 5 import java.util.Properties;
 6 
 7 public class Test {
 8     public static void main(String[] args) {
 9         Properties p = new Properties();
10         p.setProperty("id","dean");
11         p.setProperty("password","123456");
12 
13         try{
14             PrintStream fW = new PrintStream(new File("e:\\test1.xml"));
15             p.storeToXML(fW,"test");
16         } catch (IOException e) {
17             e.printStackTrace();
18         }
19     }
20 }
21                 
复制代码

 

总的来说,虽然jdk中存在date类这种特别奇葩的类,但是这些不常用的工具类还是很方便使用的,要能用起来,在用得到的时候还是很能提高效率的。比自己写解析方便快捷多了。



 

Java遍历Properties

如代码所示
[java]  view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package pkg;  
  5.   
  6. import java.util.Enumeration;  
  7. import java.util.Iterator;  
  8. import java.util.Map.Entry;  
  9. import java.util.Properties;  
  10.   
  11. /** 
  12.  * @author qefee 
  13.  *  
  14.  */  
  15. public class ShowProperties {  
  16.   
  17.     /** 
  18.      * @param args 
  19.      */  
  20.     public static void main(String[] args) {  
  21.         Properties properties = System.getProperties();  
  22.   
  23.         // show keys  
  24.         showKeys(properties);  
  25.   
  26.         // show values  
  27.         showValues(properties);  
  28.   
  29.         // show keys and values  
  30.         showKeysAndValues(properties);  
  31.     }  
  32.   
  33.     /** 
  34.      * @param properties 
  35.      */  
  36.     private static void showKeys(Properties properties) {  
  37.         Enumeration<?> enu = properties.propertyNames();  
  38.         while (enu.hasMoreElements()) {  
  39.             Object key = enu.nextElement();  
  40.             System.out.println(key);  
  41.         }  
  42.     }  
  43.   
  44.     /** 
  45.      * @param properties 
  46.      */  
  47.     private static void showValues(Properties properties) {  
  48.         Enumeration<Object> enu = properties.elements();  
  49.         while (enu.hasMoreElements()) {  
  50.             Object value = enu.nextElement();  
  51.             System.out.println(value);  
  52.         }  
  53.     }  
  54.   
  55.     /** 
  56.      * @param properties 
  57.      */  
  58.     private static void showKeysAndValues(Properties properties) {  
  59.         Iterator<Entry<Object, Object>> it = properties.entrySet().iterator();  
  60.         while (it.hasNext()) {  
  61.             Entry<Object, Object> entry = it.next();  
  62.             Object key = entry.getKey();  
  63.             Object value = entry.getValue();  
  64.             System.out.println("key   :" + key);  
  65.             System.out.println("value :" + value);  
  66.             System.out.println("---------------");  
  67.         }  
  68.     }  
  69.   
  70. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值