java - 默认文件系统位置

标签 java path

Java 中有一个默认的(文件系统?)位置。

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

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

我怎样才能得到它?

最佳答案

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

类似...

Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"));

已更新

JFileChooser 使用 FileSystemView 获取其“默认”目录

 Path path = FileSystemView.getFileSystemView().getDefaultDirectory().toPath()

同样,您也可以使用...

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"));

并测试每个,看看它们是否确实存在

关于java - 默认文件系统位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17736129/

相关文章:

java - Java 错误中的自下而上堆

java - Play Framework CryptoAES key

Angular 默认路由器 socket

java - ImageIcon 的 Mac 文件路径

python - os.path 等同于 python 中的 web url?

c - ANSI C 中的字符串处理

java - PHP 和 Java : Is setting property's visibility private and accessing it via getter from derived class overkill?

java - 是否存在我不知道的 OpenStreetMap/MapQuest 托管的 map 图 block 属性?

java - 将生成的文件输入回程序中

windows - 如何将 Windows "where"命令的输出存储在批处理文件中的变量中?