What’s the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

C:\temp\nouritFile.txt – This is a path, an absolute path, and a canonical path.
.\nouritFile.txt – This is a path. It’s neither an absolute path nor a canonical path.
C:\temp\nourit\bin\..\\..\nouritFile.txt – This is a path and an absolute path. It’s not a canonical path.
A canonical path is always an absolute path.

package com.nourit.javaIO;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class PathExample {
  private static final ArrayList<String> pathExample;
  static {
    pathExample = new ArrayList<String>();
    pathExample.add("D:/tmp/nourit/nourit.txt");
    pathExample.add("nourit/nourit.txt");
    pathExample.add("../nourit/nourit.txt");
    pathExample.add("../nourit");
  }

  public static void main(String[] args) {

    for (String path : pathExample) {
      File file = new File(path);
      System.out.println("=============================================================================");
      System.out.println("name          :" + file.getName());
      System.out.println("is directory  :" + file.isDirectory());
      System.out.println("exists        :" + file.exists());
      System.out.println("path          :" + file.getPath());
      System.out.println("absolute file :" + file.getAbsoluteFile());
      System.out.println("absolute path :" + file.getAbsolutePath());
      try {
        System.out.println("canonical file:" + file.getCanonicalFile());
        System.out.println("canonical path:" + file.getCanonicalPath());
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}

Output:

=============================================================================
name          :nourit.txt
is directory  :false
exists        :true
path          :D:\tmp\nourit\nourit.txt
absolute file :D:\tmp\nourit\nourit.txt
absolute path :D:\tmp\nourit\nourit.txt
canonical file:D:\tmp\nourit\nourit.txt
canonical path:D:\tmp\nourit\nourit.txt
=============================================================================
name          :nourit.txt
is directory  :false
exists        :false
path          :nourit\nourit.txt
absolute file :D:\Development\NOUR-IT-trunk\workspace_trunk\Java\nourit\nourit.txt
absolute path :D:\Development\NOUR-IT-trunk\workspace_trunk\Java\nourit\nourit.txt
canonical file:D:\Development\NOUR-IT-trunk\workspace_trunk\Java\nourit\nourit.txt
canonical path:D:\Development\NOUR-IT-trunk\workspace_trunk\Java\nourit\nourit.txt
=============================================================================
name          :nourit.txt
is directory  :false
exists        :false
path          :..\nourit\nourit.txt
absolute file :D:\Development\NOUR-IT-trunk\workspace_trunk\Java\..\nourit\nourit.txt
absolute path :D:\Development\NOUR-IT-trunk\workspace_trunk\Java\..\nourit\nourit.txt
canonical file:D:\Development\NOUR-IT-trunk\workspace_trunk\nourit\nourit.txt
canonical path:D:\Development\NOUR-IT-trunk\workspace_trunk\nourit\nourit.txt
=============================================================================
name          :nourit
is directory  :false
exists        :false
path          :..\nourit
absolute file :D:\Development\NOUR-IT-trunk\workspace_trunk\Java\..\nourit
absolute path :D:\Development\NOUR-IT-trunk\workspace_trunk\Java\..\nourit
canonical file:D:\Development\NOUR-IT-trunk\workspace_trunk\nourit
canonical path:D:\Development\NOUR-IT-trunk\workspace_trunk\nourit

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*