JAVA – How To Get The Current Directory

The following script gets the current directory at runtime:

import java.io.File;

public class CurrentDir {
   public static void main (String args[]) {
     File dir = new File (“.”);
     try {
       System.out.println (“Current Directory : ” + dir.getCanonicalPath());       }
     catch(Exception e) {
       e.printStackTrace();
       }
     }
}

The same logic can be used to get the parent to the current directory:

import java.io.File;

public class ParentDir {
   public static void main (String args[]) {
     File dir = new File (“..”);
     try {
       System.out.println (“Parent  Directory : ” + dir.getCanonicalPath());
       }
     catch(Exception e) {
       e.printStackTrace();
       }
     }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s