Joining paths in Java

后端 未结 6 1267
有刺的猬
有刺的猬 2021-01-30 02:48

In Python I can join two paths with os.path.join:

os.path.join(\"foo\", \"bar\") # => \"foo/bar\"

I\'m trying to

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-30 03:19

    Paths#get(String first, String... more) states,

    Converts a path string, or a sequence of strings that when joined form a path string, to a Path.

    ...

    A Path representing an empty path is returned if first is the empty string and more does not contain any non-empty strings.

    To get the current user directory you can simply use System.getProperty("user.dir").

    Path path = Paths.get(System.getProperty("user.dir"), "abc.txt");
    System.out.println(path);
    

    Moreover, get method uses variable length argument of String, which will be used to provide subsequent path strings. So, to create Path for /test/inside/abc.txt you have to use it in a following way,

    Path path = Paths.get("/test", "inside", "abc.txt");
    

提交回复
热议问题