Kamis, 17 April 2014

How to find current directory in Java with Example






How to find current directory in Java with ExampleIts easy to get current directory in Java by using built-in system property provided by Java environment. Current directory represent here the directory from where "java" command has launched. you can use "user.dir" system property to find current working directory in Java. This article is in continuation of my earlier post on Java e.g. How to load properties file in Java on XML format or How to parse XML files in Java using DOM parser and Why Java doesn’t support multiple inheritance . If you haven’t read them already, You may find them useful and worth reading. By the way here is a quick example of finding current directory in Java:


 





How to get current directory in Java with Example




public class CurrentDirectoryExample {

 

    public static void main(String args[]) {

     

        String current = System.getProperty("user.dir");

        System.out.println("Current working directory in Java : " + current);

     

    }

}









If you run above program from C:\Test it will print C:\Test as current working directory






C:\Test> java CurrentWorkingDirectoryExample

Current working directory in Java : C:\Test






If you run it from C:\ then it will print C:\ as current working directory as shown in below example






C:\> java -cp ./Test  CurrentWorkingDirectoryExample

Current working directory in Java : C:\






That's all on how to get current directory in Java. Its easy just remember name of system property  "user.dir" which gives directory from where java command has been executed.





Some other Java tips  you may like
































Source:http://javarevisited.blogspot.com/2012/04/how-to-find-current-directory-in-java.html

Tidak ada komentar:

Posting Komentar