Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java类似于Python os.path.expanduser/os.path.expandvars_Java_Python - Fatal编程技术网

Java类似于Python os.path.expanduser/os.path.expandvars

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

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 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


}///