我需要读取文件,在不关闭阅读器的情况下查找并替换同一文件中的某些文本。
建议任何简单的方法......
Iam读取文件如下。如何将其写入同一文件。
private static void readFile(String path) throws IOException {
makeFolderStructure(path);
for (String xmlNameString : xmlFileNamesList) {
FileReader xmlFile = new FileReader(
path + "\\xml\\" + xmlNameString + ".xml");
BufferedReader br = new BufferedReader(xmlFile);
try {
String line = br.readLine();
while (line != null) {
if (line.contains("<") && line.indexOf(" ") != -1) {
String tagName = line.substring(line.indexOf("<") + 1,
line.indexOf(" "));
if (line.indexOf("id") != -1) {
Pattern p = Pattern.compile("\"([^\"]*)\"");
Matcher m = p.matcher(line);
while (m.find()) {
String tagId = (m.group(1));
System.out.println(tagId+"_"+tagName);
}
}
}
line = br.readLine();
}
} finally {
br.close();
}
}
}