【发布时间】:2018-08-17 08:25:25
【问题描述】:
我使用了这段代码,它显示了我的项目目录。
System.out.println("Present Project Directory : "+ System.getProperty("user.dir"));
例如:C:\Users\Yousuf\Documents\NetBeansProjects\Store Management System
我想要做的是从通过此代码获得的目录中移回 C:\Users\Yousuf\Documents 仅两个文件夹。我该怎么办?
【问题讨论】:
-
我的 Jframe 中有一个 Jbutton,用于截取屏幕截图。现在我想将该图像保存在 Windows 中的 Documents 或 Pictures 文件夹等文件夹中,并且我不想使用固定路径因为其他 PC 可能会有所不同。
-
使用 String.split('\') 并重建您的路径...但这只是一种解决方法,以防您没有更好的解决方案。
-
是的,这就是我要问的。如何重构?
-
这取决于你的程序在哪里运行。在 Windows 上,它喜欢
"C:" + "\\" + "users"+...在 Linux 或 Mac 上,它喜欢"usr" + "/" + ... -
Path documents = Paths.get(System.getProperty("user.dir")).getParent().getParent();。请注意,这只适用于您当前的配置。如果您从其他地方启动您的应用程序,那么user.dir就不同了。