我使用JAVA bufferedreader来读取文本文件,文本文件的格式/布局是xml。我想搜索以下行:
<element id="ID">1</Element>
并将“1”替换为“2”
<property name="seed">2</property>
但似乎无法绕过它。
try {
bufferedReader = new BufferedReader( new FileReader(f));
bufferedWriter = new BufferedWriter(new FileWriter(w));
String s = "";
String seed="";
while ((s = bufferedReader.readLine()) != null ) {
if(s.contains("<element id="ID">1</Element>
")){
System.out.println(s);
s= s.replace("<element id="ID">1</Element>
", "<element id="ID">2</Element>
");
}
答案 0 :(得分:0)
if(s.contains("ID")){
s= s.replace(">1<", ">"+2+"<");
}