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

Related Tutorials


 
 

Share on Google+Share on Google+

Java Example program to get the current working directory

Advertisement
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

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2008

Related Tutorials

Discuss: Java Example program to get the current working directory  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
SAGI KARTEEKA VARMA
June 26, 2012
java

import java.io.File; class DirList { public static void main(String args[]) { String dirname = "/java"; File f1= new.File(dirname); if(f1.isDirectory()) { System.out.println("Directory of "+ dirname); String s[]=f1.list(); for(int i=0;i<s.length;i++) { File f =new File(dirname+"/"+s[i]); if(f.isDirectory()) { System.out.println(s[i]+"is a directory"); } else { System.out.println(s[i]+"is a File"); } } } else { System.out.println(dirname+"is not a directory"); } } }
ananth
October 12, 2012
current working directory in Java - tomcat context

The above code work in both Windows and Linux environments as a standalone app, as above. However, when we use it in Tomcat's context, it works only in Windows (ex: shows as 'C:/tomcat/bin' as the current working directory), however it shows up as '/' in Linux when it actually should be showing up as a '/opt/apps/Tomcat/bin'
DMCA.com