Home Java Java-get-example Java Example program to get the current working directory



Java Example program to get the current working directory
Posted on: October 23, 2008 at 12:00 AM
This example java program illustrates to get the current working directory (CWD). With the File class object we can get two types of paths in java.

Java Example program to get the current working directory

     

This example java program illustrates to get the current working directory (CWD). With the File class object we can get two types of paths in java. These are as follows:

  1. Canonical Path and
  2. Absolute Path

We can get the canonical path by calling the getCononicalPath() with the File class object and for absolute path we can use getAbsolutePath() method.

Here is the full example source code for CurrentWorkingDirectory.java  as follows:

 

CurrentWorkingDirectory.java

import java.io.File;
 public class CurrentWorkingDirectory {
 public static void main (String args[]) {
 File directory = new File (".");
 try {
 System.out.println ("Current directory's canonical path: " 
  + directory.getCanonicalPath
())
   System.out.println ("Current directory's absolute  path: " 
 
+ directory.getAbsolutePath());
 }catch(Exception e) {
 System.out.println("Exceptione is ="+e.getMessage());
  }
 }
}

 

Output:

C:\javaexamples>javac CurrentWorkingDirectory.java

C:\javaexamples>java CurrentWorkingDirectory
Current directory's canonical path: C:\javaexamples
Current directory's absolute path: C:\javaexamples\.

 

Download Source Code

 

Related Tags for Java Example program to get the current working directory:
javacfileclassobjectpathdirectorytypesgettypethispathsexamplewithworkprogramtoramexameilitcanpecurrentinasmnttrcajclesworkingdirobjprorateratestorxaxampsrectctordirectatkisirllmplcwdstrvatwssrenthavstctojepleplprogro


More Tutorials from this section

Ask Questions?    Discuss: Java Example program to get the current working directory  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.