以 Java 获取当前工作目录
java.lang.System.getProperty() 方法用于获取系统属性。该系统属性由作为该方法参数的键指定。要获取当前工作目录,所用键为 user.dir。
演示这一点的程序如下 −
示例
public class Demo {
public static void main(String[] argv) throws Exception {
String currentDirectory = System.getProperty("user.dir");
System.out.println("The current working directory is " + currentDirectory);
}
}上述程序的输出如下 −
输出
The current working directory is c:\JavaProgram
现在,让我们来理解上述程序。
通过结合 java.lang.System.getProperty() 方法和键 user.dir 获得当前工作目录。然后打印当前工作目录。演示这一点的代码片段如下 −
String currentDirectory = System.getProperty("user.dir");
System.out.println("The current working directory is " + currentDirectory);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP