架构:
Get properties:
public static Properties getProperties(String fileName) {
String filePath = "src/resource/" + fileName + ".properties";
File pFile = new File(filePath);
FileInputStream pInStream = null;
try {
pInStream = new FileInputStream(pFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Properties p = new Properties();
try {
p.load(pInStream);
} catch (IOException e) {
e.printStackTrace();
}
// p.list(System.out);
return p;
}
取值:
public static void transform(Connection conn, ResultSet rs, String propertyName, String rawField,
String newField) {
int score = 0;
Properties p = getProperties(propertyName);
Enumeration<?> enu = p.propertyNames();
while(enu.hasMoreElements()) {
String thisKey = (String)enu.nextElement();
try {
if (rawField.contains(thisKey)) {
score = Integer.parseInt(p.getProperty(thisKey));
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(score);
update(conn, rs, newField, score);
}
写值:
public static void updateProperties(String fileName, List<String> updateList) {
String filePath = "src/crawl/" + fileName + ".properties";
File pFile = new File(filePath);
FileOutputStream pOutStream = null;
try {
pOutStream = new FileOutputStream(pFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Properties p = new Properties();
try {
for (String updateValue:updateList) {
p.setProperty(updateValue,""); //key, value
}
p.store(pOutStream, null);
pOutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
参考:
http://blog.csdn.net/shixing_11/article/details/5652347