How to get current working directory in java

In this post, we will see how to get current working directory in java.
There are four ways to do it.

Using property user.dir

We can use System.getProperty(“user.dir”) to get current working directory path.
When I ran above program, I got below output:

Using getAbsolutePath()

We can use file.getAbos
When I ran above program, I got below output:

Using Paths.get() (Java 7+)

You can use Paths.get("") method get current working directory in java.

When I ran above program, I got below output:

Using FileSystems.getDefault() (Java 7+)

You can use FileSystem.getDefault() method get current working directory in java.

When I ran above program, I got below output:

That’s all about how to get current working directory in java.

Was this post helpful?

Related Posts

  • 16 September

    How to Remove Extension from Filename in Java

    Table of ContentsWays to Remove extension from filename in javaUsing substring() and lastIndexOf() methodsUsing replaceAll() methodUsing Apache common library In this post, we will see how to remove extension from filename in java. Ways to Remove extension from filename in java There are multiple ways to remove extension from filename in java. Let’s go through […]

  • 15 September

    How to Get Temp Directory Path in Java

    Table of ContentsGet Temp Directory Path in JavaUsing System.getProperty()By Creating Temp File and Extracting Temp PathOverride Default Temp Directory Path In this post, we will see how to get temp directory path in java. Get Temp Directory Path in Java Using System.getProperty() To get the temp directory path, you can simply use System.getProperty("java.io.tmpdir"). It will […]

  • Convert OutputStream to byte array in java
    16 February

    Convert Outputstream to Byte Array in Java

    Table of ContentsConvert OutputStream to Byte array in JavaConvert OutputStream to ByteBuffer in Java In this post, we will see how to convert OutputStream to Byte array in Java. Convert OutputStream to Byte array in Java Here are steps to convert OutputStream to Byte array in java. Create instance of ByteArrayOutputStream baos Write data to […]

  • 08 August

    Difference between Scanner and BufferReader in java

    Table of ContentsIntroductionScannerBufferedReaderDifference between Scanner and BufferedReader In this post, we will see difference between Scanner and BufferReader in java. Java has two classes that have been used for reading files for a very long time. These two classes are Scanner and BufferedReader. In this post, we are going to find major differences and similarities […]

  • 29 March

    Read UTF-8 Encoded Data in java

    Table of ContentsUsing Files’s newBufferedReader()Using BufferedReaderUsing DataInputStream’s readUTF() method In this post, we will see how to read UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. There are multiple ways to read UTF-8 Encoded Data […]

  • 29 March

    Write UTF-8 Encoded Data in java

    Table of ContentsUsing Files’s newBufferWriter()Using BufferedWriterUsing DataOutputStream’s writeUTF() method In this post, we will see how to write UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. We will use Hindi language sentences to write in […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.