How to get the current working directory in Java?

前端 未结 22 3467
时光说笑
时光说笑 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 07:58

    I've found this solution in the comments which is better than others and more portable:

    String cwd = new File("").getAbsolutePath();
    

    Or even

    String cwd = Paths.get("").toAbsolutePath();
    

提交回复
热议问题