/**
* 把 版权信息 写入到 根目录下的 copyInfo.txt文件中
* @param str
*/
public void writer(String infoPath, String str) {
PrintWriter pw;
try {
HttpServletRequest request = ServletActionContext.getRequest();
String basePath=request.getServletContext().getRealPath("copyInfo.txt");
pw = new PrintWriter( new FileWriter( basePath ) );
pw.print(str);
pw.close();
} catch (IOException e) {
logger.error("文件写操作,configInfoAction.writer", e);
}
}
/**
* 读取 根目录下的 copyInfo.txt 文件中的 信息
*/
public String reader(String infoPath) {
HttpServletRequest request = ServletActionContext.getRequest();
String basePath=request.getServletContext().getRealPath("copyInfo.txt");
FileReader fr;
String temp="";
try {
fr = new FileReader(basePath);
BufferedReader br=new BufferedReader(fr);
String line=null;
while((line=br.readLine())!=null){
temp+=line;
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
logger.error("读版权信息-文件是否存在异常,configInfoAction.reader", e);
} catch (IOException e) {
logger.error("读版权信息-IO,configInfoAction.reader", e);
}
return temp;
}
/**
* 向property文件中写入信息
* @param path
* @param key
* @param value
* @throws IOException
*/
public void setProperty(String path, String key, String value) throws IOException {
props.setProperty(key,value);//设置属性值,如不属性不存在新建
//props.setProperty("testProperty","testPropertyValue");
FileOutputStream out;
out = new FileOutputStream(path);
props.store(out,"");//设置属性头,如不想设置,请把后面一个用""替换掉
out.flush();//清空缓存,写入磁盘
out.close();//关闭输出流
}
/**
* 获得配置文件中的 版权信息
* @return
*/
public static String getCopyInfo() {
//获得属性文件路径
HttpServletRequest request = ServletActionContext.getRequest();
String basePath=request.getServletContext().getRealPath("/WEB-INF/src/setting.properties");
PropertyUtil pu=new PropertyUtil(basePath);
System.out.println(pu.getProperty("CopyInfo"));
return pu.getProperty("CopyInfo");
}
private static Properties props=new Properties();
public PropertyUtil(String path){
InputStream is=null;
try{
is=new FileInputStream(path);
props.load(is);
}catch(Exception e){
throw new RuntimeException(e);
}finally{
if(null!=is){
try{
is.close();
}catch(IOException e){
throw new RuntimeException(e);
}
}
}
}
public static String getProperty(String key){
String property=props.getProperty(key);
return property;
}