//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;
}
}