Java 默认文件系统位置

Java 默认文件系统位置,java,path,Java,Path,Java中有一个默认(文件系统?)位置 例如,当您实例化JFileChooser而未指定要在其中打开的文件夹时,它将在该默认位置打开 我需要将该默认位置作为路径对象(不使用JFileChooser,这样做只是为了解释) 我怎样才能得到它?不确定这是否是您要找的。。。对于JFileChooser,默认目录通常是Windows上的“我的文档”文件夹,Unix上是用户的主目录 如果需要工作目录的路径,请选择CurrentClass.class.getProtectionDomain().getCode

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

例如,当您实例化
JFileChooser
而未指定要在其中打开的文件夹时,它将在该默认位置打开

我需要将该默认位置作为
路径
对象(不使用JFileChooser,这样做只是为了解释)


我怎样才能得到它?

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


如果需要工作目录的路径,请选择CurrentClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()。

不确定这是否是您要查找的路径。。。对于JFileChooser,默认目录通常是Windows上的“我的文档”文件夹,Unix上是用户的主目录


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

您应该能够从
System.getProperty(“user.home”)
创建一个
路径,它是用户的主目录

类似于

Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"));
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"));
已更新

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

 Path path = FileSystemView.getFileSystemView().getDefaultDirectory().toPath()
同样地,你也可以使用像

Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"));
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"));

然后测试每一个,看看它们是否确实存在

您应该能够从
System.getProperty(“user.home”)
创建一个
路径,该路径是用户的主目录

类似于

Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"));
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"));
已更新

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

 Path path = FileSystemView.getFileSystemView().getDefaultDirectory().toPath()
同样地,你也可以使用像

Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"));
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"));

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

我认为这个问题可能重复你应该能够从
System.getProperty(“user.dir”)
创建一个
路径,它是用户的家directory@Karlovsky120添加了一个可能的示例,无需测试,但是它应该让你开始可能的重复我认为这个问题重复你应该能够从
System.getProperty(“user.dir”)
创建一个
路径,它是用户的家directory@Karlovsky120加上一个可能的例子,没有测试,但它应该让你开始实际上,这不是我想要的。我希望它打开我的文档,而不是打开类文件的位置。。。我想让它打开我的文档。。。用户主目录,不在程序范围内,但在整个操作系统范围内…应为
user.home
。您可以查找“我的文档”以查看它是否存在,但请记住,其他操作系统没有“我的文档”文件夹。。。我正在尝试将其与JFileChooser同步。如果用户打开一个新文档,则将保存该文档的路径,无论该路径是什么。但是,如果用户从头开始创建一个新文档,我需要保存文档的某种路径,这将是默认的JFileChooser路径。JFileChooser如何获得它的默认源代码?这正是我所需要的。我可以手动访问我的文档,但我希望它与平台无关事实上,那不是我想要的。我希望它打开我的文档,而不是打开类文件的位置。。。我想让它打开我的文档。。。用户主目录,不在程序范围内,但在整个操作系统范围内…应为
user.home
。您可以查找“我的文档”以查看它是否存在,但请记住,其他操作系统没有“我的文档”文件夹。。。我正在尝试将其与JFileChooser同步。如果用户打开一个新文档,则将保存该文档的路径,无论该路径是什么。但是,如果用户从头开始创建一个新文档,我需要保存文档的某种路径,这将是默认的JFileChooser路径。JFileChooser如何获得它的默认源代码?这正是我所需要的。我可以手动访问我的文档,但我希望它与平台无关“CurrentClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()”-不会
System.getProperty(“user.dir”)
返回相同的内容吗?“CurrentClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()”-不会
System.getProperty(“user.dir”)
返回相同的内容吗?