This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
A quote from Khalid Mugal's book (Chapter 18-Files&Streams,page 552) For example, if the File object represented the absolute pathname "c:\book\ chapter1" on Windows, then this pathname would be returned by these methods.On the other hand, if the File object represented the relative pathname "..\book\chapter1" and the current directory had the absolute pathname "c:\documents", the pathname returned by the getPath(), getAbsolutePath() and getCanonicalPath() methods would be "..\book\chapter1", "c:\documents\..\book\chapter1" and "c:\book\chapter1", respectively. BTW, you can download this chapter in PDF. That's all. Jamal
getAbsolutePath returns the absolute i,e not relative path of the file or directory. For example, the following code creates a file 'test' in the directory directly above the current directory which is C:\java\io File f1 = new File("..", "test.txt") f1.createNewFile(); System.out.println( f1.getAbsolutePath() ); Output: C:\java\io\..\test the canonical path is the same as the absolute path BUT all relative indicators . and .. are resolved For example, System.out.println( f1.getCanonicalPath() ); Output: // '..' in absolute path is resolved C:\java\test getPath converts the abstract pathname into a pathname string. For example, System.out.println( f1.getPath() ); Output: ..\test Hope this helps......
In short, remember this - 1) getPath() returns whatever was passed to the onstructor. 2) getAbsolutePath() returns the current working directory path + whatever was passed to the constructor, as if it is just a plain string concatenation. 3)getCanonicalPath() works similar to getAbsolutePath() but it resolves . to current directory and .. to parent directory, and return the resulting path, much similar to the way relative URLs get resolved to absolute URLs. HTH, - Manish