Is it possible to find and replace in a Path object without converting it to a String?
For example here is a simple code snippet,
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
final Path wholePath = Paths.get("/usr/animals/data/donkey/folder1/folder2/data.txt");
final Path find = Paths.get("/usr/animals/data/donkey");
final Path replace = Paths.get("/usr/animals/data/sym4");
Path expected = Paths.get("/usr/animals/data/sym4/folder1/folder2/data.txt"); // how to get this?
Path path = Paths.get(wholePath.toString().replace(find.toString(), replace.toString()));
System.out.println(expected.equals(path));
}
}
Using a .toString() method seems to be a non-portable way and instead it's better to use the .resolve() or similar method?
Any suggestions?
.getParent().getParent().getParent().getParent().resolve(newDirName).resolve(...)