java 解析.properties或.cfg文件的方法

CFG文件格式: 
大多数情况下,很多程序都要保存用户的设置,办法有很多:注册表,日志文件...... 而很多程序都使用了一个专用的文件。为了方便起见,常常命名为xxx.cfg,有时甚至直接命名为Config.cfg。 
这只是一个为开发及使用方便而"发明"的一个后缀名。所以,这种文件没有固定的格式,其实也并不能算作是一种文件类型。用途也仅仅是保存用户的设置,平常没有必要打开。 

可以使用java里的preperties类来读取这种键值对,如cfg文件,properties文件等 
示例: 
Java代码   收藏代码
  1. public class TestMain {  
  2.    
  3.  //根据key读取value  
  4.  public static String readValue(String filePath,String key) {  
  5.   Properties props = new Properties();  
  6.         try {  
  7.          InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
  8.          props.load(in);  
  9.          String value = props.getProperty (key);  
  10.             System.out.println(key+value);  
  11.             return value;  
  12.         } catch (Exception e) {  
  13.          e.printStackTrace();  
  14.          return null;  
  15.         }  
  16.  }  
  17.    
  18.  //读取properties的全部信息  
  19.     public static void readProperties(String filePath) {  
  20.      Properties props = new Properties();  
  21.         try {  
  22.          InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
  23.          props.load(in);  
  24.             Enumeration en = props.propertyNames();  
  25.              while (en.hasMoreElements()) {  
  26.               String key = (String) en.nextElement();  
  27.                     String Property = props.getProperty (key);  
  28.                     System.out.println(key+Property);  
  29.                 }  
  30.         } catch (Exception e) {  
  31.          e.printStackTrace();  
  32.         }  
  33.     }  
  34.   
  35.     //写入properties信息  
  36.     public static void writeProperties(String filePath,String parameterName,String parameterValue) {  
  37.      Properties prop = new Properties();  
  38.      try {  
  39.       InputStream fis = new FileInputStream(filePath);  
  40.             //从输入流中读取属性列表(键和元素对)  
  41.             prop.load(fis);  
  42.             OutputStream fos = new FileOutputStream(filePath);  
  43.             prop.setProperty(parameterName, parameterValue);  
  44.             //以适合使用 load 方法加载到 Properties 表中的格式,  
  45.             //将此 Properties 表中的属性列表(键和元素对)写入输出流  
  46.             prop.store(fos, "Update '" + parameterName + "' value");  
  47.         } catch (IOException e) {  
  48.          System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");  
  49.         }  
  50.     }  
  51.   
  52.     public static void main(String[] args) {  
  53.      readValue("info.properties","url");  
  54.         writeProperties("info.properties","age","21");  
  55.         readProperties("info.properties" );  
  56.         System.out.println("OK");  
  57.     }  
  58. }  
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值