代理 IP 一 (存取orProperties 的读写)
package spider.xxxxxx.common;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
/**
*
* @author jxialiang
*
*/
public class PropertiesUtil {
public Properties getProps() {
// 得到配置文件的值
Properties props = new Properties();
try {
props.load(getClass().getResourceAsStream("/proxy.properties"));
} catch (Exception e) {
}
return props;
}
public String readValue(String filePath, String key) {
Properties props = getProps();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public Enumeration getKeys(String filePath) {
Enumeration en;
Properties props = getProps();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
en = props.propertyNames();
/*
while(en.hasMoreElements()){
String key = (String)en.nextElement();
String property = props.getProperty(key);
//for testing purpose
System.out.println(key+property);
return en;
}
* */
return en;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
//写入properties信息
public void writeProperties(String filePath, String parameterName,
String parameterValue) {
Properties prop = getProps();
FileOutputStream w = null;
try {
w = new FileOutputStream(filePath);
if (null == prop) {
prop = new Properties();
prop.load(new FileInputStream(filePath));
}
prop.setProperty(parameterName, parameterValue);
prop.store(w, parameterName);
w.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != w) {
w.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读取properties的全部信息
public List readProperties(String filePath) {
List list = new ArrayList();
try {
Enumeration en = this.getKeys(filePath);
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String property = this.readValue(filePath, key);
list.add(key + ":" + property);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return list;
}
public void removeProperty(String filePath, String key) {
Properties props = getProps();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
props.remove(key);
OutputStream fos = new FileOutputStream(filePath);
props.store(fos, "Last Update, Deletion: key:" + key);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void updateProperty(String filePath, String key, String value) {
Properties props = getProps();
try {
InputStream in = new BufferedInputStream(new FileInputStream(
filePath));
props.load(in);
props.setProperty(key, value);
OutputStream fos = new FileOutputStream(filePath);
props.store(fos, "Last Update, key:" + key + ",value: " + value);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public boolean checkKeyAvailability(String filePath, String key) {
boolean flag = true;
Enumeration keys = this.getKeys(filePath);
while (keys.hasMoreElements()) {
String keyStr = (String) keys.nextElement();
if (keyStr.equalsIgnoreCase(key)) {
flag = false;
}
}
return flag;
}
}