properties例子

properties操作实例:

注意根目录是指项目下的目录,假如项目名叫testProj,那么放在根目录下就是指放在testProj下面。

这个例子只要在testProj下面新建一个配置文件info.properties即可。


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值