BufferedReader在具有属性的标记之间读取值并替换它

时间:2016-03-05 15:42:31

标签: java bufferedreader

我使用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>
    ");

                }

1 个答案:

答案 0 :(得分:0)

        if(s.contains("ID")){

            s= s.replace(">1<", ">"+2+"<");


        }