首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >默认FileSystem位置

默认FileSystem位置
EN

Stack Overflow用户
提问于 2013-07-19 08:14:10
回答 2查看 2.5K关注 0票数 2

有一个默认值(FileSystem?)Java中的位置。

例如,当您实例化一个JFileChooser而没有指定要在其中打开哪个文件夹时,它将在该默认位置打开。

我需要将这个默认位置作为一个Path对象(不使用JFileChooser,只是为了解释)。

我怎么才能得到它呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-19 08:49:15

您应该能够从用户的主目录System.getProperty("user.home")创建一个Path

就像..。

代码语言:javascript
复制
Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"));

已更新

JFileChooser使用FileSystemView来获取它的“默认”目录

代码语言:javascript
复制
 Path path = FileSystemView.getFileSystemView().getDefaultDirectory().toPath()

同样,您也可以使用类似于...

代码语言:javascript
复制
Path docs = FileSystems.getDefault().getPath(System.getProperty("user.home"), "Documents");
Path myDocs = FileSystems.getDefault().getPath(System.getProperty("user.home"), "My Documents");
Path userHome = FileSystems.getDefault().getPath(System.getProperty("user.home"));

并测试每一个,看看它们是否真的存在

票数 3
EN

Stack Overflow用户

发布于 2013-07-19 08:19:04

不确定这是否是你要找的东西...对于JFileChooser,默认目录通常是“我的文档”文件夹,而在Unix上则是用户的主目录。Source

如果您想要工作目录的路径,那么CurrentClass.class.getProtectionDomain().getCodeSource().getLocation().getPath().

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17736129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档