Home Java Java-get-example Get Current Directory

Related Tutorials


 
 

Share on Google+Share on Google+

Get Current Directory

Advertisement
In this Tutorial we want to describe you a code that helps you in understanding Get Current Directory. For this we have a class name CurrentDir, Inside the main method we create an instance file1 and file2 of file class by using new operator.

Get Current Directory

     

In this Tutorial we want to describe you a code that helps you in understanding Get Current Directory. For this we have a class name CurrentDir, Inside the main method we create an instance file1 and file2 of file class by using new operator. The try block includes  println method used to print the current directory and parent directory using following methods-

get Canonical Path( ) This method return the path of the directory.

The println is used to print the Current  and Parent dir.If there is an exception in try block ,the catch block handle the exception and print it on the command prompt.

 

 

 

Current Dir.java
import java.io.*;

public class CurrentDir {

  public static void main(String args[]) {

  File file1 = new File(".");
  File file2 = new File("..");
 try {
 System.out.println("Current dir : " + file1.getCanonicalPath());
   System.out.println("Parent  dir : " + file2.getCanonicalPath());
  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

Current dir : C:\Documents and Settings
\Administrator\My Documents\NetBeansProjects\GetExample


Parent  dir : C:\Documents and Settings
\Administrator\My Documents\NetBeansProjects
 

Download 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: November 5, 2008

Related Tutorials

Discuss: Get Current Directory  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com