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



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());



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

© 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0441 seconds.