In Java 7 and higher, you can use the Paths.get()
method to get the current working directory in Java:
// get the current working directory
String cwd = Paths.get("").toAbsolutePath().toString();
// print cwd
System.out.println(cwd);
Alternatively, you can also use System.getProperty("user.dir")
to get the current working directory:
// get the current working directory
String cwd = System.getProperty("user.dir");
// print cwd
System.out.println(cwd);
Further Reading
You may be interested in other Java I/O articles:
- Reading and Writing Files in Java
- How to Read and Write Text Files in Java
- How to Read and Write Binary Files in Java
- Reading and Writing Files using Java NIO API
- How to read a file line by line in Java
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.