• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

files problem - absolute/canonical

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can't really figure out the difference between the getAbsolutePath() and getCanonicalPath().
Also, what is the meaning of isAbsolute() ?
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
An absolute path is the path name that starts with a '\' or follows the volume name(eg: g .
A relative path is the one that starts with ..\dir\subdir\filename
Now suppose that our current working directory is g:\dir
consider this program:
import java.io.*;
public class AbsCan
{
public static void main(String str[])
{
File f=new File(str[0]);
System.out.println("Path = "+f.getPath());
System.out.println("AbsolutePath = "+f.getAbsolutePath());
try
{
System.out.println("CanonicalPath = "+f.getCanonicalPath());
//System.out.println("isabs? = "+f.isAbsolute());
}
catch(IOException ie)
{
System.out.println("************"+ie);
}
}
}
If we run this program with the command line argument
g:\dir>java AbsCan g:\dir\subdir\filename
The output is
g:\dir\subdir\filename for all the three print statements.
If we run this program like
g:\dir>java AbsCan ..\subdir\filename
Then the output will be
Path =..\subdir\filename
AbsolutePath =g:\dir\..\subdir\filename
//Note: cwd concatenated with cdm line argument
CanonicalPath =g:\subdir\filename
//Note: relative references all resolved.
//But why & where to use this "resolved reference"? Beats me!
isAbsolute() returns true if the path is absolute.(ie the first case)
Good Luck!
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it priya... i was also experimenting with the stuff along the same lines.
thanx for your time...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic