Java - 使用System.getProperty(" user.dir")获取主目录 [英] Java - using System.getProperty("user.dir") to get the home directory

查看:1160
本文介绍了Java - 使用System.getProperty(" user.dir")获取主目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否使用:

System.getProperty("user.dir");

获取文件夹的绝对路径是最好的方法吗?我希望将我的应用程序传递到其他计算机上,我需要一个完整的证明方式来获取'home'目录,这样我只需要在我需要使用其他文件夹时添加到路径上:

to get the absolute path of a folder is the best way to go about it? I am looking to pass my application onto other computers and I need a full proof way of getting the 'home' directory so that I can just add onto the path when I need to use other folders by just doing:

String path = System.getProperty("user.dir");
String otherFolder = path + "\\other";


推荐答案

获取当前用户的主目录的方式是

way of getting home directory of current user is

String currentUsersHomeDir = System.getProperty("user.home");

并附加路径分隔符

String otherFolder = currentUsersHomeDir + File.separator + "other";

File.separator


系统相关的默认名称分隔符,为方便起见,表示为字符串。此字符串包含单个字符,即separatorChar。

The system-dependent default name-separator character, represented as a string for convenience. This string contains a single character, namely separatorChar.

这篇关于Java - 使用System.getProperty(" user.dir")获取主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆