How to parse file name in Java?
java
Solution
Using Java 9+
Path jarPath = Paths.get("/opt/test/myfolder/myinsidefolder/myfile.jar");
Path xmlPath = jarPath.resolveSibling("Test.xml");
Using Java 8 and older
File myfile = new File("/opt/.../myinsidefolder/myfile.jar");
File test = new File(myfile.getParent(), "Test.xml");
Or, if you prefer working with strings only:
String f = "/opt/test/myfolder/myinsidefolder/myfile.jar";
f = new File(new File(f).getParent(), "Test.xml").getAbsolutePath();
System.out.println(f); // /opt/test/myfolder/myinsidefolder/Test.xml
Problem
I have a java file path `/opt/test/myfolder/myinsidefolder/myfile.jar` I want to replace the file path to here root path will remain same but want to change file name from `myfile.jar` to `Test.xml` `/opt/test/myfolder/myinsidefolder/Test.xml` How can i do this in java any help?