How To: Find Parent Directory in Java

The Problem

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

Analysis

The file system (both Linux/Unix and Windows) use the special directory name .. (two dots) 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 Java class ParentDir exposes only one public service, the getParentDir() method.

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

See also: How To: Find the Current Directory in Java

Back to Java How To

 
java/howtos/findparentdirectory.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