How To: Find Current Directory in Java

The Problem

The client code needs to know what is the current directory.

Analysis

The file system (both Linux/Unix and Windows) use the special directory name . (single dot) as a reference to the current directory. We can use the getCanonicalPath() method found in File class to retrieve the directory name.

The Solution

The following class CurrentDir exposes only one public service, the getCurrentDir() method.

import java.io.File;
import java.io.IOException;
class currentDir {
	public static String getCurrentDir() 
		throws IOException 
	{
		File dir = new File(".");
		return dir.getCanonicalPath();
	}
}

See also: How To: Find Parent Directory in Java

Back to Java How To

 
java/howtos/findcurrentdirectory.txt · Last modified: 2009/10/31 23:39 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki