How to get the current working directory in Java?

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

    Code :

    public class JavaApplication {
      public static void main(String[] args) {
        System.out.println("Working Directory = " + System.getProperty("user.dir"));
      }
    }
    

    This will print the absolute path of the current directory from where your application was initialized.


    Explanation:

    From the documentation:

    java.io package resolve relative pathnames using current user directory. The current directory is represented as system property, that is, user.dir and is the directory from where the JVM was invoked.

提交回复
热议问题