Java Properties 类读取配置文件信息

Java Properties 类读取配置文件信息在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。从中可以看到如何读取properties文件,并应用读取出来的值,是学习操作properties文件的好例子。 
  • 获取JVM的系统属性

Java代码   收藏代码
  1. import java.util.Properties;  
  2.    
  3. class PropTest {  
  4.     public static void main(String[] args) {  
  5.     Properties pps = System.getProperties();  
  6.     pps.list(System.out);  
  7.     }  
  8. }  

引用
结果: 
-- listing properties -- 
java.runtime.name=Java(TM) 2 Runtime Environment, Stand... 
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_04\jre... 
java.vm.version=1.5.0_04-b05 
java.vm.vendor=Sun Microsystems Inc. 
java.vendor.url=http://java.sun.com/ 
path.separator=; 
java.vm.name=Java HotSpot(TM) Client VM 
file.encoding.pkg=sun.io 
user.country=CN 
sun.os.patch.level=Service Pack 2 
java.vm.specification.name=Java Virtual Machine Specification 
...以下略

  • 读取配置文件

配置文件config.ini 
Xml代码   收藏代码
  1. author=ZJ  
  2. user=all  
  3. copyright=2006-2007   


Java代码   收藏代码
  1. import java.io.FileInputStream;  
  2. import java.util.Enumeration;  
  3. import java.util.Properties;  
  4.    
  5. class PropTest {  
  6.     public static void main(String[] args) {  
  7. Properties pps=new Properties();   
  8.     try {  
  9.         pps.load(new FileInputStream("config.ini"));  
  10.         Enumeration enum1 = pps.propertyNames();  
  11.         while (enum1.hasMoreElements()) {  
  12.        String strKey = (String) enum1.nextElement();  
  13.        String strValue = pps.getProperty(strKey);  
  14.        System.out.println(strKey + "=" + strValue);  
  15.         }  
  16.     } catch (Exception e) {  
  17.         e.printStackTrace();  
  18.     }  
  19.     }  
  20. }  

引用
结果: 
user=all 
copyright=2006-2007 
author=ZJ


  • 另外一个用JAVA操作properties文件的实例

Java代码   收藏代码
  1. package control;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.util.Enumeration;  
  10. import java.util.Properties;  
  11.   
  12. public class TestMain {  
  13.    
  14.  //根据key读取value  
  15.  public static String readValue(String filePath,String key) {  
  16.   Properties props = new Properties();  
  17.         try {  
  18.          InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
  19.          props.load(in);  
  20.          String value = props.getProperty (key);  
  21.             System.out.println(key+value);  
  22.             return value;  
  23.         } catch (Exception e) {  
  24.          e.printStackTrace();  
  25.          return null;  
  26.         }  
  27.  }  
  28.    
  29.  //读取properties的全部信息  
  30.     public static void readProperties(String filePath) {  
  31.      Properties props = new Properties();  
  32.         try {  
  33.          InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
  34.          props.load(in);  
  35.             Enumeration en = props.propertyNames();  
  36.              while (en.hasMoreElements()) {  
  37.               String key = (String) en.nextElement();  
  38.                     String Property = props.getProperty (key);  
  39.                     System.out.println(key+Property);  
  40.                 }  
  41.         } catch (Exception e) {  
  42.          e.printStackTrace();  
  43.         }  
  44.     }  
  45.   
  46.     //写入properties信息  
  47.     public static void writeProperties(String filePath,String parameterName,String parameterValue) {  
  48.      Properties prop = new Properties();  
  49.      try {  
  50.       InputStream fis = new FileInputStream(filePath);  
  51.             //从输入流中读取属性列表(键和元素对)  
  52.             prop.load(fis);  
  53.             //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。  
  54.             //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。  
  55.             OutputStream fos = new FileOutputStream(filePath);  
  56.             prop.setProperty(parameterName, parameterValue);  
  57.             //以适合使用 load 方法加载到 Properties 表中的格式,  
  58.             //将此 Properties 表中的属性列表(键和元素对)写入输出流  
  59.             prop.store(fos, "Update '" + parameterName + "' value");  
  60.         } catch (IOException e) {  
  61.          System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");  
  62.         }  
  63.     }  
  64.   
  65.     public static void main(String[] args) {  
  66.      readValue("info.properties","url");  
  67.         writeProperties("info.properties","age","21");  
  68.         readProperties("info.properties" );  
  69.         System.out.println("OK");  
  70.     }  
  71. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值