从配置文件中读取和写入数据

   //1、读写函数:

*//**

    * 存储token和当前时间
    * @param token
    *//*
    public void storeToken(String token) 
    {        
   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String createtime = df.format(new Date());
   //获取当前系统时间
   ConfigUtils.getInstance().setConfig("token", token);
   ConfigUtils.getInstance().setConfig("tokenTime", createtime);

    *//**
     * 存储ticket并记录创建时间
     *//*
    public void storeTicket(TicketResponse ticketresponse)
    {
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
     String createtime_ticket = df.format(new Date());    
   
    ticketIn[0]=ticketresponse.getErrcode();
    ticketIn[1]=ticketresponse.getErrmsg();
    ticketIn[2]=ticketresponse.getTicket();
    ticketIn[3]=String.valueOf(ticketresponse.getExpires_in());
    ticketIn[4]=createtime_ticket;


    ConfigUtils.getInstance().setConfig("ticket", ticketresponse.getTicket());
    ConfigUtils.getInstance().setConfig("ticketTime", createtime_ticket);
     
   

    }


//读取文件

 /*  private String getTokenFromFile() {
    //获取文件中的token值
    String tokenOut =  ConfigUtils.getInstance().getConfig("token", "");    
    return tokenOut;    
}
    */


//2、配置文件处理类ConfigUtils:

package com.cn.bjca.utils;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;


/**
 * 配置文件工具类
 * 
 * @author GuanXu
 *
 */
public class ConfigUtils {


private static final String CONFIG_FILE_NAME = "application.properties";


private static ConfigUtils instance;

private ConfigUtils() {
super();
}







/**
* 获取配置单例
* @return ConfigUtils 实例
*/
public static ConfigUtils getInstance()
{
if(instance==null)
instance=new ConfigUtils();

return instance;
}


/**
* 获取配置信息
* @param configName 配置名称
* @param defValue 默认值
* @return 配置
*/
public String getConfig(String configName, String defValue) {
String ret = null;
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream(CONFIG_FILE_NAME);


Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e) {
return defValue;
}
ret = p.getProperty(configName);
if(ret==null)
{
ret=defValue;
}


return ret;
}

/**
* 获取整形的配置信息
* @param configName 配置名称
* @param defValue 默认值
* @return 配置
*/
public int getConfigInt(String configName,String defValue)
{
String retStr=getConfig(configName,defValue);
return Integer.parseInt(retStr);
}


/**
* 修改配置文件
* @param configName
* @param value
* @return
*/
public boolean setConfig(String configName, String value) {
boolean ret = true;
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream(CONFIG_FILE_NAME);


Properties p = new Properties();
try {
p.load(inputStream);
inputStream.close();
URL url = this.getClass().getClassLoader().getResource("");

FileOutputStream fos = new FileOutputStream(url.getPath() + CONFIG_FILE_NAME, true);
p.setProperty(configName, value);
p.store(fos,"注释");
fos.close();
} catch (IOException e) {
ret = false;
}
return ret;

}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值