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/