• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic