简体   繁体   English

new File() 与 Java 中的环境变量

[英]new File() with environment variables in Java

How to get a File java object from its path string with environments vars?如何从带有环境变量的路径字符串中获取文件 java object? For example:例如:

new File("%SYSTEM_ROOT%/file.txt")

or或者

new File("${SYSTEM_ROOT}/file.txt")

Syntax that allows getting value of environment variable using % sign is a feature of windows shell.允许使用%符号获取环境变量值的语法是 windows shell 的一个特性。 In java you should use System.getenv("SYSTEM_ROOT") instead, so your line should look like: new File(System.getenv("SYSTEM_ROOT") + "/file.txt") or even better new File(new File(System.getenv("SYSTEM_ROOT"), "file.txt")在 java 你应该使用System.getenv("SYSTEM_ROOT")代替,所以你的行应该看起来像: new File(System.getenv("SYSTEM_ROOT") + "/file.txt")甚至更好new File(new File(System.getenv("SYSTEM_ROOT"), "file.txt")

I know this Question is a little bit old, but no Answer yet.我知道这个问题有点老了,但还没有答案。

In another Question about environment variables there is a simliar Problem and they have a sample code for regex:在另一个关于环境变量的问题中,有一个类似的问题,他们有一个正则表达式的示例代码:
System Variable Regex系统变量正则表达式

This code replaces all system vars that are in the format ${ENV_VAR}.此代码替换格式为 ${ENV_VAR} 的所有系统变量。

Hope that helps,希望有帮助,
Kalasch卡拉施

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM