Java 替换文件中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3147615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:52:12  来源:igfitidea点击:

Replace string in file

javaio

提问by Dónal

I'm looking for a way to replace a string in a file without reading the whole file into memory. Normally I would use a Reader and Writer, i.e. something like the following:

我正在寻找一种方法来替换文件中的字符串,而无需将整个文件读入内存。通常我会使用 Reader 和 Writer,即类似于以下内容:

public static void replace(String oldstring, String newstring, File in, File out)
    throws IOException {

    BufferedReader reader = new BufferedReader(new FileReader(in));
    PrintWriter writer = new PrintWriter(new FileWriter(out));
    String line = null;
    while ((line = reader.readLine()) != null)
        writer.println(line.replaceAll(oldstring,newstring));

    // I'm aware of the potential for resource leaks here. Proper resource
    // handling has been omitted in the interest of brevity
    reader.close();
    writer.close();
}

However, I want to do the replacement in-place, and don't think I can have a Reader and Writer open on the same file concurrently. Also, I'm using Java 1.4, so dont't have access to NIO, Scanner, etc.

但是,我想就地进行替换,并且不认为我可以同时在同一个文件上打开 Reader 和 Writer。另外,我使用的是 Java 1.4,因此无法访问 NIO、Scanner 等。

Thanks, Don

谢谢,唐

采纳答案by Jakob Borg

"In place" replacing usually isn't possible for files, unless the replacement is exactly the same length as the original. Otherwise the file would need to either grow, thus shuffling all later bytes "to the right", or shrink. The common way of doing this is reading the file, writing the replacement to a temporary file, then replacing the original file with the temporary.

文件的“就地”替换通常是不可能的,除非替换的长度与原始文件的长度完全相同。否则文件要么需要增长,从而将所有后面的字节“向右”改组,要么缩小。这样做的常用方法是读取文件,将替换写入临时文件,然后用临时文件替换原始文件。

This also has the advantage that the file in question is at all times either in the original state or in the completely replaced state, never in between.

这还有一个优点,即所涉及的文件始终处于原始状态或完全替换状态,绝不会介于两者之间。

回答by Andreas Dolk

Replacing something in a file or stream requires a Writer or OutputStream which is capable of deleting and inserting bytes at any position. A replace operation can be split into a deleteand an insertoperation.

替换文件或流中的某些内容需要能够在任何位置删除和插入字节的 Writer 或 OutputStream。替换操作可以拆分为删除插入操作。

Looking at the API of OutputStream and Writer I can't find suitable methods. One could use OutputStream to write bytes with an offset but this will simply overwrite existing context. So it could work for the special case, that original and replacement have the equal length.

查看 OutputStream 和 Writer 的 API 找不到合适的方法。可以使用 OutputStream 写入带有偏移量的字节,但这只会覆盖现有的上下文。所以它可以用于特殊情况,原始和替换具有相同的长度。

So right now I think, writing the edited lines to a temporary file and replacing the orignal file with the temp file afterwards is still the best solution.

所以现在我认为,将编辑过的行写入临时文件,然后用临时文件替换原始文件仍然是最好的解决方案。