Propertes文件的读取和保存_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2249 | 回复: 0   主题: Propertes文件的读取和保存        下一篇 
    本主题由 hui.chen 于 2015-7-30 15:23:14 移动
unknsnow
注册用户
等级:上士
经验:272
发帖:78
精华:0
注册:2012-2-10
状态:离线
发送短消息息给unknsnow 加好友    发送短消息息给unknsnow 发消息
发表于: IP:您无权察看 2015-7-28 17:31:51 | [全部帖] [楼主帖] 楼主

/**
* Update the key's value to new value ,and write back to the Configuration
* file
*
* @param configFilePath
* Configuration file
* @param key
* the key
* @param value
* the new value
*/
publicstatic Boolean updateProperty(String configFilePath, String key, String value) {
      // Read File
      BufferedReader reader = null;
      try {
            reader = new BufferedReader(new FileReader(configFilePath));
      } catch (FileNotFoundException e) {
            Log.getInstance().fine("Can not find file: " + configFilePath + "."); //$NON-NLS-1$ //$NON-NLS-2$
      }
      StringBuilder builder = new StringBuilder();
      String tempLine;
      String tempValue;
      // Find the property and update
      try {
            while ((tempLine = reader.readLine()) != null) {
                  if (tempLine.trim().startsWith(COMMENT_FALG)) {
                        builder.append(tempLine).append(LINE_SEPARATOR);
                        continue;
                  }
                  if (tempLine.contains(key)) {
                        tempValue = tempLine.trim().substring(key.length());
                        if (tempValue.trim().startsWith(EQUAL_FLAG)) {
                              tempLine = key + EQUAL_FLAG + value;
                        }
                  }
                  builder.append(tempLine).append(LINE_SEPARATOR);
            }
            reader.close();
      } catch (IOException e) {
            Log.getInstance().fine("Read configuration file: " + configFilePath + " error."); //$NON-NLS-1$ //$NON-NLS-2$
            e.printStackTrace();
      }
      // Update File
      BufferedWriter writer = null;
      try {
            writer = new BufferedWriter(new FileWriter(configFilePath));
            writer.append(builder.toString());
            writer.close();
      } catch (IOException e) {
            Log.getInstance().fine("Update configuration file: " + configFilePath + " error."); //$NON-NLS-1$ //$NON-NLS-2$
      }
      returntrue;
}
/**
* Get value from configuration file by key
*
* @param configFilePath
* configuration file path
*
* @param key
* the key
*
* @return the value
*/
publicstatic String getProperty(String configFilePath, String key) {
      Properties prop = new Properties();
      try {
            prop.load(new FileInputStream(configFilePath));
      } catch (FileNotFoundException e) {
            Log.getInstance().fine("Can not find file: " + configFilePath + "."); //$NON-NLS-1$ //$NON-NLS-2$
      } catch (IOException e) {
            Log.getInstance().fine("Error read configuration file: " + configFilePath + " ."); //$NON-NLS-1$ //$NON-NLS-2$
      }
      return prop.getProperty(key);
}


--转自 北京联动北方科技有限公司



该贴由hui.chen转至本版2015-7-30 15:23:14



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论