文件内容查找替换

  • Post author:
  • Post category:其他


很费劲写的一个文件内容查找替换功能,结果没用上,保存留用

特点是使用流操作文件,支持正则匹配

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) {


}

}



版权声明:本文为Uoooo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。