Java类似于Python os.path.expanduser/os.path.expandvars
Java中有Python的Java类似于Python os.path.expanduser/os.path.expandvars,java,python,Java,Python,Java中有Python的os.path.expanduser和os.path.expandvars()这样的东西吗 如果没有,是否有这样的库呢?我不知道有什么能完全满足您的需求,但是您可以通过System.getProperty(“user.home”)获得用户的home dir,并且您可以使用来解析环境变量。很遗憾,Java没有附带“电池”:( import java.util.regex.Matcher; import java.util.regex.Pattern; public cl
os.path.expanduser
和os.path.expandvars
()这样的东西吗
如果没有,是否有这样的库呢?我不知道有什么能完全满足您的需求,但是您可以通过
System.getProperty(“user.home”)
获得用户的home dir,并且您可以使用来解析环境变量。很遗憾,Java没有附带“电池”:(
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Path {
static Pattern pv=Pattern.compile("\\$\\{(\\w+)\\}");
/*
* os.path.expanduser
*/
public static String expanduser(String path) {
String user=System.getProperty("user.home");
return path.replaceFirst("~", user);
}//expanduser
/*
* os.path.expandvars
*/
public static String expandvars(String path) {
String result=new String(path);
Matcher m=pv.matcher(path);
while(m.find()) {
String var=m.group(1);
String value=System.getenv(var);
if (value!=null)
result=result.replace("${"+var+"}", value);
}
return result;
}//expandvars
}///