package util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class UpdateScanInterval {
/**
* 指定property文件
*/
//生成项目中的路径
private String path;
private File staticfile;
public UpdateScanInterval() {
path = this.getClass().getResource("/").getPath().substring(1, this.getClass().getResource("/").getPath().length()) + "util/scanInterval.properties";
try {
path = java.net.URLDecoder.decode(path,"utf-8"); //对路径解码,防止特殊符号产生的乱码
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
staticfile = new File(path);
}
/**
* 根据Key 读取Value
*
* @param key
* @return
*/
public String readData(String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(path));
props.load(in);
in.close();
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 修改或添加键值对 如果key存在,修改 反之,添加。
*
* @param key
* @param value
*/
public void writeData(String key, String value) {
Properties prop = new Properties();
try {
File file = new File(staticfile.getAbsolutePath());
if (!file.exists())
file.createNewFile();
InputStream fis = new FileInputStream(file);
prop.load(fis);
fis.close();// 一定要在修改值之前关闭fis
OutputStream fos = new FileOutputStream(staticfile.getAbsolutePath());
prop.setProperty(key, value);
prop.store(fos, "\u8BBE\u7F6E\u626B\u63CF\u95F4\u9694\u65F6\u95F4");
fos.close();
} catch (IOException e) {
System.err.println("Visit " + path + " for updating "
+ value + " value error");
}
}
public static void main(String[] args) {
try {
String p = "E:/Tomcat%206.0/webapps/";
p = java.net.URLDecoder.decode(p,"utf-8");
System.out.println("1>>>>"+p);
p = java.net.URLDecoder.decode(p,"utf-8");
System.out.println("2>>>>"+p);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// // TODO Auto-generated method stub
// UpdateScanInterval test = new UpdateScanInterval();
// test.writeData("scanInterval", "1000*60*10");
// String scanInterval = test.readData("scanInterval");
// System.out.println("The name of the person is:" + scanInterval);
}
}