• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

File Class

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm a bit confused over the methods of the File class: getAbsolutePath, getCanonicalPath, getPath.
What's the difference to the 3 above?
Hope someone could help me out.
Thanks.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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......
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, guys.
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic