Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Program to print current directory path to the console

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post Program to print current directory path to the console

    This code will print the current working directory to the console.

    Example output:

    Current dir : \\aserver\homedirs\ab\My Documents\Java Workspace\JPF
    Parent dir : \\aserver\homedirs\ab\My Documents\Java Workspace
    import java.io.File;
     
    public class CurrentDir {
     
        public static void main(String args[]) {
            File dir1 = new File(".");
            File dir2 = new File("..");
            try {
                System.out.println("Current dir : " + dir1.getCanonicalPath());
                System.out.println("Parent  dir : " + dir2.getCanonicalPath());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Location
    Franklin, MA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Get the current directory path

    Note that if you are using a method which takes a String for the directory name, you can use "." to get the current directory. For example

    Last edited by billca42; October 9th, 2009 at 01:01 PM.
    Bill C (billca42)

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. How to create directory in Java?
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 24th, 2011, 12:53 PM
  3. [SOLVED] How to a set java class path?
    By captjade in forum Java Theory & Questions
    Replies: 1
    Last Post: March 10th, 2009, 06:40 AM
  4. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM

Tags for this Thread