Joining paths in Java

后端 未结 6 1132
有刺的猬
有刺的猬 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:39

    The most basic way is:

    Path filepath = Paths.get("foo", "bar");
    

    You should never write Paths.get(""). I'm surprised that works at all. If you want to refer to the current directory explicitly, use Paths.get(System.getProperty("user.dir")). If you want the user's home directory, use Paths.get(System.getProperty("user.home")).

    You can also combine the approaches:

    Path filepath = Paths.get(
        System.getProperty("user.home"), "data", "foo.txt");
    

提交回复
热议问题