2012-06-27 36 views
1

我需要在java中添加用戶配置文件變量,顯然%userprofile%不起作用。當我嘗試將它輸入到代碼底部的目錄中時,它不接受它。但是我可以在命令行中回顯我的%userprofil%如何正常添加userprofile變量%userprofile%

userprofile +「\ Downloads \ file.txt」);

+0

你想要做什麼.. – Ahmad

回答

6
String System.getenv(String var) 

所以

String userprofile = System.getenv("USERPROFILE"); 

然後操縱作爲defired(如附加的路徑的其餘部分...)

另外,獲得與Map<String,String> System.getenv()

+0

我到底會如何實現這一目標? – jerhynsoen

+0

明白了! static String userprofile = System.getenv(「USERPROFILE」); – jerhynsoen

3

整個環境你可能希望獲得當前用戶的主目錄。在Java中,通常通過調用System.getProperty("user.home")來完成。但在Windows中,這可能會被分配給%USERPROFILE%,這使事情變得複雜一些。見this bug。作爲一種解決方法,您可能想要執行John3136建議的操作:System.getenv("USERPROFILE"),但它不適用於其他環境。

+0

'System.getProperty(「user.home」)',而不是'System.getenv(「user.home」)' – bancer

+0

謝謝,更新了它。 – n0rm1e