Difference between getAbsolutePath and getCanonicalPath

getCanonicalPath() resolves relative paths. It also restores original capitalization. You can use canonical paths to determine if two paths are the same, eg. c:windows..autoexec.bat and c:autoexec.bat will be equal if you get the path using getCanonicalPath but different with getAbsolutePath.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      printPath("autoexec.bat");
      printPath("c:\windows\system32\.\cloud.gif");
      printPath("c:\windows\..\autoexec.bat");
      printPath("c:\windows");
   }
 
   public static void printPath(String s) {
      try {
         System.out.println(s);
         System.out.println("tgetAbsolutePath()t" + new File(s).getAbsolutePath());
         System.out.println("tgetCanonicalPath()t" + new File(s).getCanonicalPath());
      }
      catch(IOException e) {
         System.out.println(e); 
      }
   }
}

outputs:

autoexec.bat
	getAbsolutePath()		C:\autoexec.bat
	getCanonicalPath()	C:autoexec.bat
c:windowssystem32.cloud.gif
	getAbsolutePath()		c:windowssystem32.cloud.gif
	getCanonicalPath()	C:WINDOWSSYSTEM32cloud.gif
c:windows..autoexec.bat
	getAbsolutePath()		c:windows..autoexec.bat
	getCanonicalPath()	C:autoexec.bat
c:windows
	getAbsolutePath()		c:windows
	getCanonicalPath()	C:WINDOWS