Java操作properties文件

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


http://blog.csdn.net/caohaicheng/article/details/21031763

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值