• 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

JCP

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please tell me the difference between getAbsolutePath() and getCanonicalPath() in Files with an simple example, which shows the difference
 
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,
This Qn has been dealt with earlier.Here's a jist of my previous posting regarding the same Qn.
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.
 
Jait Thomas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your reply. I will run the program and try to understand.
reply
    Bookmark Topic Watch Topic
  • New Topic