How to resolve file names in Java?

I have a java file path

/opt/test/myfolder/myinsidefolder/myfile. jar

I want to replace the file path with the root path here, which will remain the same, but I want to change the file name from myfile Change jar to test xml

/opt/test/myfolder/myinsidefolder/Test. xml

How can I do anything to help in Java?

Solution

This is the right way:

File myfile = new File("/opt/.../myinsidefolder/myfile.jar");
File test = new File(myfile.getParent(),"Test.xml");

Or, if you prefer to use only strings:

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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>