Java 如何添加userprofile变量%userprofile%
我需要在java中添加一个用户配置文件变量,显然%userprofile%不起作用。当我尝试将其输入代码底部的目录时,它不接受它。但是,我可以在命令行中回显我的%userprofil% userprofile+“\Downloads\file.txt”) 所以 然后按违抗的方式进行操作(例如,附加路径的其余部分…)Java 如何添加userprofile变量%userprofile%,java,Java,我需要在java中添加一个用户配置文件变量,显然%userprofile%不起作用。当我尝试将其输入代码底部的目录时,它不接受它。但是,我可以在命令行中回显我的%userprofil% userprofile+“\Downloads\file.txt”) 所以 然后按违抗的方式进行操作(例如,附加路径的其余部分…) 或者,使用Map System.getenv()获取整个环境。您可能希望获取当前用户的主目录。在Java中,通常通过调用System.getProperty(“user.home”)
或者,使用Map System.getenv()获取整个环境。您可能希望获取当前用户的主目录。在Java中,通常通过调用
System.getProperty(“user.home”)
来实现。但在Windows中,这可能会分配给%USERPROFILE%,这使得事情有点复杂。看见作为一种解决方法,您可能希望按照John3136的建议执行:System.getenv(“USERPROFILE”)
,但它不可移植到其他环境。您到底想做什么..我该如何实现它?明白了!静态字符串userprofile=System.getenv(“userprofile”)System.getProperty(“user.home”)
,而不是System.getenv(“user.home”)
String System.getenv(String var)
String userprofile = System.getenv("USERPROFILE");