How to get the current working directory in Java?

前端 未结 22 3091
时光说笑
时光说笑 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:11

    I'm on Linux and get same result for both of these approaches:

    @Test
    public void aaa()
    {
        System.err.println(Paths.get("").toAbsolutePath().toString());
    
        System.err.println(System.getProperty("user.dir"));
    }
    

    Paths.get("") docs

    System.getProperty("user.dir") docs

提交回复
热议问题