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