默认文件系统位置

时间:2013-07-19 00:14:10

标签: java path

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

E.g。当您实例化JFileChooser而未指定要打开的文件夹时,它将在该默认位置打开。

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

我怎样才能得到它?

2 个答案:

答案 0 :(得分:3)

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

像...一样的东西。

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

测试每一个以确定它们是否真的存在

答案 1 :(得分:1)

不确定这是否是您要找的...对于JFileChooser,默认目录通常是Windows上的“My Documents”文件夹,以及Unix上用户的主目录。 Source

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