如何通过读取文本文件替换java中两个字符串之间的字符串

如何通过读取文本文件替换java中两个字符串之间的字符串,java,Java,我想读一个包含网站的文本文件。现在我想替换http://to/here后面的字符串 hello.com应替换为localhost.com,每个行中包含网站: package StringReplace.StringReplace;import java.io.*;import java.lang.*;public class stringreplace { public static void main(String[] args) throws Exception { File s

我想读一个包含网站的文本文件。现在我想替换http://to/here后面的字符串


hello.com应替换为localhost.com,每个
行中包含网站:

package StringReplace.StringReplace;import java.io.*;import java.lang.*;public class stringreplace {
public static void main(String[] args) throws Exception {

    File src = new File("input.txt");
    BufferedReader br = new BufferedReader(new FileReader(src));

    String st;
    while ((st = br.readLine()) != null)
        System.out.println(st);
    st = st.replaceAll("[^,]*,\\*{5}", "X,*****");
    System.out.println(st);

}}

编辑:在您的示例中,行定义为
st

URI uri = new URI(line);
String replaced = new URI(uri.getScheme(), "localhost.com", uri.getPath(), uri.getFragment()).toString();

您的任务包括几个步骤,但最后一个步骤没有明确说明:

您是想更改文件中URI中的主机,还是只想读取文件内容并在RAM中操作它们,以便将它们用于其他用途

但是,此示例获取包含网站URI的文件的路径,通过流式传输每行(假设文件格式为每行一个URI),从每行创建一个
URI
对象,并使用原始的方案创建一个新的
URI
实例,但将指定的不同主机放入:

// [...]
while ((st = br.readLine()) != null) {
   URI uri = new URI(st);
   String replaced = new URI(uri.getScheme(), "localhost.com", uri.getPath(), uri.getFragment()).toString();
   System.out.println(replaced);
}
// [...] 
我的试用文件中的文件内容如下:

public static void main(String[] args) {
    // define the file location
    String pathToFile = "C:\\temp\\websites.txt";
    // make it a java.nio.file.Path
    Path filePath = Paths.get(pathToFile);
    // define the new host for all the URIs
    String exchangeHost = "localhost.com";

    try {
        // stream the lines
        Files.lines(filePath)
                .forEach(line -> {
                    try {
                        // create a URI from the original
                        URI originalUri = new URI(line);
                        // and a new one using scheme of the original, but put the new host
                        URI updatedUri = new URI(originalUri.getScheme(), exchangeHost, 
                                originalUri.getPath(), originalUri.getFragment());
                        // print this change to the console
                        System.out.println(originalUri.toString()
                                            + "  ————>  "
                                            + updatedUri.toString());
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                });
    } catch (IOException e) {
        // handle IOException
        e.printStackTrace();
    }
}
程序的输出如下:

http://www.somewhere.com/hello
http://somewhere.de/hallo
https://www.somewhere.co.uk/hello
https://somewhere.ru/privet
http://www.somewhere.es/hola

您能给我们展示一下您想要解析和处理的文件内容的示例吗?看看
String
提供的方法。查看
indexOf
substring
。或者
replaceAll
,如果您熟悉正则表达式。可能重复@deHaar,可能是正则表达式狂热者
http://www.somewhere.com/hello  ————>  http://localhost.com/hello
http://somewhere.de/hallo  ————>  http://localhost.com/hallo
https://www.somewhere.co.uk/hello  ————>  https://localhost.com/hello
https://somewhere.ru/privet  ————>  https://localhost.com/privet
http://www.somewhere.es/hola  ————>  http://localhost.com/hola