• Post Reply Bookmark Topic Watch Topic
  • New Topic

getCanonicalPath() and getAbsolutePath()

 
Taps
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried the above two methods using all possible forms of the File object .
eg. File f = new File("abc.txt")
File f = new File("\\dir1\\abc.txt")
File f = new File("..\\dir1\\abc.txt")
Can someone tell me exactly what these 2 methods do ?
I seem to get unexplainable answers for all options when the aboce file "abc.txt" exists and when it doesnt !
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Taps",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getAbsolutePath() gives you the real path to the file. So if the file lives in /usr/local/home, then saying getAbsolutePath() will return "/usr/local/home/file.txt"
getCanonicalPath() is more tricky and system-dependent. Basically getCanonicalPath() will resolve any alias to the real file. On Windows and Macs, getAbsolutePath() will do this also, so they return the same thing, but to make your code as platform independent as possible, you would use getCanonicalPath() if you needed the real path. So if you hava a path "/bin/perl" that links to the real file at "/usr/local/bin/perl" then getCanonicalPath will return "/usr/local/bin/perl" while getAbsolutePath() will return "/bin/perl" on a Unix machine.
By the way, I got that from O'Rileys book on I/O.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!