如何在Java中获取URL对象内路径的父级?
我在Java应用程序中遇到以下问题 通过此语句,我检索应用程序的当前位置:如何在Java中获取URL对象内路径的父级?,java,url,jakarta-ee,directory,Java,Url,Jakarta Ee,Directory,我在Java应用程序中遇到以下问题 通过此语句,我检索应用程序的当前位置: URL location = Main.class.getProtectionDomain().getCodeSource().getLocation(); 因此,例如,我的位置包含如下路径:/C:/Projects/edi sta/out/production/edi sta/ 好的,我怎样才能轻松获得父路径 例如,考虑到前面的路径,如何获得****/C:/Projects/edi sta/out/productio
URL location = Main.class.getProtectionDomain().getCodeSource().getLocation();
因此,例如,我的位置包含如下路径:/C:/Projects/edi sta/out/production/edi sta/
好的,我怎样才能轻松获得父路径
例如,考虑到前面的路径,如何获得****/C:/Projects/edi sta/out/production/**路径(即最后一个edi sta/文件夹的直接父级)
Java为我提供了一些开箱即用的功能,或者让我实现字符串操作?使用标准Java中的方法。您可以使用URI#resolve(string)
获得指向父级的URL
:
URL parentUrl = location.toURI().resolve("..").toURL(); // .. for parent
我无法在URL对象上使用getParentFile()。我要怎么做才能使用它?