Get Current File Path In Java

 Posted On  | Yashwant Chavan 

In this tutorial we will learn how to get current file path In Java.Refer below code to display the current directory path. There are two way to get the current directory path either get it through new File(".").getCanonicalPath() method. Here dot represents the current directory. And another way is using user.dir property. System.getProperty("user.dir").

Current Directory Path Example

package com.technicalkeeda.app;

import java.io.File;
import java.io.IOException;

public class CurrentDirectoryPathExample {

    public static void main(String[] args) {
        try {
            String canonicalPath = new File(".").getCanonicalPath();
            System.out.println("Current directory path using canonical path method :- " + canonicalPath);

            String usingSystemProperty = System.getProperty("user.dir");
            System.out.println("Current directory path using system property:- " + usingSystemProperty);

        } catch (IOException e) {
            System.out.println("IOException Occured" + e.getMessage());
        }
    }

}

Output

Current directory path using canonical path method :- C:\javaworkspace\JavaExamples
Current directory path using system property:- C:\javaworkspace\JavaExamples


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0334 seconds.