简体   繁体   English

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

[英]how to replace string between two strings in java by reading a text file

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);

}}

I want to read a text file which contains websites.我想阅读一个包含网站的文本文件。 So, now I want to replace the string after http:// to /here所以,现在我想将 http:// 之后的字符串替换为 /here

http://hello.com/nnn hello.com should be replaced with localhost.com http://hello.com/nnn hello.com 应替换为 localhost.com

Your task involves several steps, while the last one is not clearly explained:您的任务涉及几个步骤,而最后一个没有明确解释:

Do you want to change the host in the URIs inside the file or do you just want to read the file content and manipulate them in the RAM only in order to use them for anything else?您是想更改文件内 URI 中的主机,还是只想读取文件内容并在 RAM 中操作它们,以便将它们用于其他用途?

However, this example takes a path to a file containing website URIs, goes through each line (assuming the file is formatted as one URI per line) by streaming them, creates a URI object from each one and a new URI instance using the scheme of the original but puts the specified different host:但是,此示例获取包含网站 URI 的文件的路径,通过流式处理每一行(假设文件被格式化为每行一个 URI),从每个文件创建一个URI object,并使用以下方案创建一个新的URI实例原始但放置指定的不同主机:

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();
    }
}

The file content in my try-out file is this:我的试用文件中的文件内容是这样的:

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

and the output of the program is the following:程序的output如下:

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

For each line with website in it:对于其中包含网站的每一line

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

URI documentation URI 文档

edit: In your example the lines are defined as st :编辑:在您的示例中,这些行定义为st

// [...]
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);
}
// [...] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM