架构:
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.youkuaiyun.com/shixing_11/article/details/5652347
本文详细介绍了Java中如何通过文件操作获取属性文件内容,使用正则表达式匹配特定属性,进行数值提取,并更新数据库数据。同时展示了如何将提取的数据写入新的属性文件,涉及属性文件读取、数据处理及文件写入的基本Java操作。
1297

被折叠的 条评论
为什么被折叠?



