java替换文本文件中的特定字符串

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

java replace specific string in textfile

javafile-iotext-files

提问by Pindo

I've got a text file called log.txt It's got the following data

我有一个名为 log.txt 的文本文件它有以下数据

1,,Mon May 05 00:05:45 WST 2014,textFiles/a.txt,images/download.jpg
2,,Mon May 05 00:05:45 WST 2014,textFiles/a.txt,images/download.jpg

the numbers before the first comma are indexes that specify each item.

第一个逗号之前的数字是指定每个项目的索引。

what I want to do is read the file then replace one part of the string(e.g. textFiles/a.txt) in a given line with another value(e.g. something/bob.txt).

我想要做的是读取文件,然后用另一个值(例如 something/bob.txt)替换给定行中字符串的一部分(例如 textFiles/a.txt)。

this is what i have so far

这是我迄今为止所拥有的

    File log= new File("log.txt");
                    String search = "1,,Mon May 05 00:05:45 WST 2014,textFiles/a.txt,images/download.jpg;
                    //file reading
                    FileReader fr = new FileReader(log);
                    String s;
                    try (BufferedReader br = new BufferedReader(fr)) {

                        while ((s = br.readLine()) != null) {
                            if (s.equals(search)) {
                                //not sure what to do here
                            }
                        }
                    }

采纳答案by Jason

One approach would be to use String.replaceAll():

一种方法是使用String.replaceAll()

File log= new File("log.txt");
String search = "textFiles/a\.txt";  // <- changed to work with String.replaceAll()
String replacement = "something/bob.txt";
//file reading
FileReader fr = new FileReader(log);
String s;
try {
    BufferedReader br = new BufferedReader(fr);

    while ((s = br.readLine()) != null) {
        s.replaceAll(search, replacement);
        // do something with the resulting line
    }
}

You could also use regular expressions, or String.indexOf()to find where in a line your search string appears.

您还可以使用正则表达式,或String.indexOf()查找搜索字符串在一行中的出现位置。

回答by laune

A very simple solution would be to use:

一个非常简单的解决方案是使用:

s = s.replace( "textFiles/a.txt", "something/bob.txt" );

To replace all occurrences, use replaceAllshown in another proposal, where a regular expression is used - take care to escape all magic characters, as indicated there.

要替换所有出现的内容,请使用replaceAll另一个提案中显示的内容,其中使用了正则表达式 - 请注意转义所有魔术字符,如此处所示。

回答by Sujan Shrestha

You could create a string of total file content and replace all the occurrence in the string and write to that file again.

您可以创建一个包含总文件内容的字符串并替换字符串中出现的所有内容并再次写入该文件。

You could something like this:

你可以是这样的:

File log= new File("log.txt");
String search = "textFiles/a.txt";
String replace = "replaceText/b.txt";

try{
    FileReader fr = new FileReader(log);
    String s;
    String totalStr = "";
    try (BufferedReader br = new BufferedReader(fr)) {

        while ((s = br.readLine()) != null) {
            totalStr += s;
        }
        totalStr = totalStr.replaceAll(search, replace);
        FileWriter fw = new FileWriter(log);
    fw.write(totalStr);
    fw.close();
    }
}catch(Exception e){
    e.printStackTrace();
}