很费劲写的一个文件内容查找替换功能,结果没用上,保存留用
特点是使用流操作文件,支持正则匹配
private void logPathReplace(String configFile, String local) {
String temp = null;
try {
File file = new File(configFile);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该行前面的内容
Pattern p = Pattern.compile("log4j//.appender//..*//.File=");
for (; (temp = br.readLine()) != null;) {
if (p.matcher(temp).find()) {
temp = temp.substring(0, temp.indexOf("=") + 1)
+ local + temp.substring(temp.indexOf("=") + 1);
}
buf = buf.append(temp);
buf = buf.append(System.getProperty("line.separator"));
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
}
}