The client code needs to know what is the current directory.
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 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