To get the absolute and canonical paths of a file, use the following two methods:
f.getAbsolutePath(); f.getCanonicalPath();
If the pathname used to construct the File object is not absolute, the getAbsolutePath() method uses the working directory to get the absolute path.
getCanonicalPath() throws IOException.
import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) { String workingDir = System.getProperty("user.dir"); System.out.println("Working Directory: " + workingDir); printFilePath("dummy.txt"); printFilePath(".." + File.separator + "notes.txt"); }/*from ww w . j a v a 2 s . c o m*/ public static void printFilePath(String pathname) { File f = new File(pathname); System.out.println("File Name: " + f.getName()); System.out.println("File exists: " + f.exists()); System.out.println("Absolute Path: " + f.getAbsolutePath()); try { System.out.println("Canonical Path: " + f.getCanonicalPath()); } catch (IOException e) { e.printStackTrace(); } } }