0
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://hello.com/nnn hello.com should be replaced with localhost.com

4
  • Could you show us a sample of the file content you want to parse and process? Commented Oct 4, 2019 at 10:04
  • Take a look at the methods that String offers. Take a look at indexOf and substring. Or replaceAll if you are familiar with regex. Commented Oct 4, 2019 at 10:04
  • Possible duplicate of replace String with another in java Commented Oct 4, 2019 at 10:17
  • stackoverflow.com/questions/40955324/… Commented Oct 4, 2019 at 10:19

1 Answer 1

0

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?

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:

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:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.