How to get the current working directory in Java?

前端 未结 22 3084
时光说笑
时光说笑 2020-11-21 07:16

I want to access my current working directory using java.

My code :

 String current = new java.io.File( \".\" ).getCanonicalPath();
        System.ou         


        
22条回答
  •  借酒劲吻你
    2020-11-21 08:14

    This will give you the path of your current working directory:

    Path path = FileSystems.getDefault().getPath(".");
    

    And this will give you the path to a file called "Foo.txt" in the working directory:

    Path path = FileSystems.getDefault().getPath("Foo.txt");
    

    Edit : To obtain an absolute path of current directory:

    Path path = FileSystems.getDefault().getPath(".").toAbsolutePath();
    

    * Update * To get current working directory:

    Path path = FileSystems.getDefault().getPath("").toAbsolutePath();
    

提交回复
热议问题