使用java替换文件中的String



我有一个TXT文件,我想改变这个字符串

<!DOCTYPE Publisher PUBLIC "-//Springer-Verlag//DTD A++ V2.4//EN" "http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd">

到这个<!DOCTYPE Publisher>使用Java。

我写了下面的函数,但它似乎不工作。

public void replace() {
try {
    File file = new File("/home/zakaria/Bureau/PhD/test2/file.txt");
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String line = "", oldtext = "";
    while((line = reader.readLine()) != null) {
       oldtext += line + "n";
    }
    reader.close();
    String newtext = oldtext
      .replaceAll("<!DOCTYPE PublishernPUBLIC "-//Springer-Verlag//DTD A++ V2.4//EN" "http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd">", 
      "<!DOCTYPE Publisher>");
    FileWriter writer = new FileWriter("/home/zakaria/Bureau/PhD/test2/file.txt");
    writer.write(newtext);
    writer.close();
  } catch (IOException ioe) {
    ioe.printStackTrace();
  }
}

我做错了什么?

试试下面这个简单的代码:

public static void replace() {
    try {
        File file = new File("resources/abc.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = "", oldtext = "";
        boolean found = false;
        while ((line = reader.readLine()) != null) {
            if (line.trim().startsWith("<!DOCTYPE Publisher")) {
                found = true;
            }
            if (line.trim().endsWith("A++V2.4.dtd">")) {
                oldtext += "<!DOCTYPE Publisher>";
                found = false;
                continue;
            }
            if (found) {
                continue;
            }
            oldtext += line + "n";
        }
        reader.close();
        FileWriter writer = new FileWriter("resources/file.txt");
        writer.write(oldtext);
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

你很幸运,因为它根本没有改变任何东西。

否则你会丢失原始文件…

永远不要原地修改文件!!

创建一个临时文件,将修改后的内容写入其中,然后再将其重命名为原始文件。

另外,你想要替换的字符串非常复杂,你不想使用.replace(),因为这将替换所有出现的字符串。

这样做:

final String quoted 
    = Pattern.quote("<!DOCTYPE PublishernPUBLIC "-//Springer-Verlag//DTD A++ V2.4//EN" "http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd">");
final Pattern pattern = Pattern.compile(quoted);
final Path victim = Paths.get("/home/zakaria/Bureau/PhD/test2/file.txt");
final Path tmpfile = Files.createTempFile("tmp", "foo");
final byte[] content = Files.readAllBytes(victim);
final String s = new String(content, StandardCharsets.UTF_8);
final String replacement = pattern.matcher(s).replaceFirst("<!DOCTYPE Publisher>");
try (
    final OutputStream out = Files.newOutputStream(tmpfile);
) {
    out.write(replacement.getBytes(StandardCharsets.UTF_8));
    out.flush();
}
Files.move(tmpfile, victim);

如果要消除的文本位于第二行及后续行,如demo-input

<!DOCTYPE Publisher
PUBLIC "-//Springer-Verlag//DTD A++ V2.4//EN"     
"http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd">

并且标记中的第一行和最后一行之间不包含关闭>,那么您可以执行以下操作:

while(more lines to process)
   if "<!DOCTYPE Publisher" is not found
      read line and output it
   else
      //This is the first line in a <!DOCTYPE tag
      read the line and output it, appending '>' to the end
      while the next line does NOT end with a '>'
         discard it (don't output it)

试试这个regexp:

String newtext = oldtext.replaceAll(
       "<!DOCTYPE PublishernPUBLIC "-\/\/Springer-Verlag\/\/DTD A[+][+] V2[.]4\/\/EN"[ ]"http:\/\/devel[.]springer[.]de\/A[+][+]\/V2[.]4\/DTD\/A[+][+]V2[.]4[.]dtd">", "<!DOCTYPE Publisher>");

唯一的变化是转义正斜杠,并在方括号之间加上点和加号。

相关内容

  • 没有找到相关文章

最新更新