java 如何使用Java替换文本文件中的一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27528676/
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-11-02 11:57:00  来源:igfitidea点击:

How to replace a line in a text file using Java?

javatextreplace

提问by MisterIbbs

I have a text file with various key value pairs separated with a '--'. Below is the code I have so far

我有一个文本文件,其中包含用“--”分隔的各种键值对。下面是我到目前为止的代码

        File file = new File("C:\StateTestFile.txt");
        BufferedWriter out = new BufferedWriter(file.getAbsolutePath());
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
           if(line.contains("content_style")) {
               //Write to the line currently reading and save back to the file
           }
        }
        br.close();

        out.close();

What I would like to do is read this text file and replace the value of a specific line with something I specify. So id want to find the 'content_style' line and replace 'posh' with 'dirty'.

我想做的是读取此文本文件并用我指定的内容替换特定行的值。所以我想找到 'content_style' 行并将 'posh' 替换为 'dirty'。

How can I do this?

我怎样才能做到这一点?

回答by Dinal24

simply use:

只需使用:

 line = line.replaceAll("posh", "dirty"); // as strings are immutable in java

回答by Damian Yerrick

This can be done in-place on a single file only if you are sure that the string you are replacing is exactly the same length in bytesas the string that replaces it. Otherwise, you can't add or delete characters in a single file, but you can create a new file.

仅当您确定要替换的字符串与替换它的字符串的字节长度完全相同时,才可以在单个文件上就地完成此操作。否则,您无法在单个文件中添加或删除字符,但可以创建一个新文件。

  1. Open the source file for reading.
  2. Open the destination file for writing.
  3. Read each line in the source file, use replaceAll, and write it to the destination file.
  4. Close both files.
  1. 打开源文件进行阅读。
  2. 打开目标文件进行写入。
  3. 读取源文件中的每一行,使用replaceAll并将其写入目标文件。
  4. 关闭这两个文件。

Alternate method that preserves the key-value semantics:

保留键值语义的替代方法:

  1. Open the source file for reading.
  2. Open the destination file for writing.
  3. Split each line in the source file into a key and value pair. If the key equals "content_style", write the key and "dirty"to the destination file. Otherwise write the key and value.
  4. Close both files.
  1. 打开源文件进行阅读。
  2. 打开目标文件进行写入。
  3. 将源文件中的每一行拆分为一个键值对。如果密钥等于"content_style",则将密钥和写入"dirty"目标文件。否则写入键和值。
  4. 关闭这两个文件。

Finally, delete the old file and rename the new file on top of the old one. If you're going to be doing key-value manipulations often, and you don't want to write out a new file all the time, it might be worth it to use a database. Look for a JDBC driver for SQLite.

最后,删除旧文件并在旧文件之上重命名新文件。如果您要经常进行键值操作,并且不想一直写出一个新文件,那么使用数据库可能是值得的。为 SQLite寻找JDBC 驱动程序