2

As I understand the different getPath,getAbsolutePath and getCanonicalPath what I don't understand why does absolute path is different from Canonical path - in the Canonical path I see /private prefix which doesn't exist in absolute path

I run the below code in scala

object ScalaDemo {
    def main(args: Array[String]): Unit = {
      val dir = Files.createTempDirectory("test").toFile

      println("dir.getPath:" + dir.getPath)
      println("dir.getAbsolutePath:" + dir.getAbsolutePath)
      println("dir.getCanonicalPath:" + dir.getCanonicalPath)
   }
}

I got this output:

dir.getPath:/var/folders/fq/8q8jpphd2qb2_07p4h2kzn0m0000gn/T/test3084029786797422592
dir.getAbsolutePath:/var/folders/fq/8q8jpphd2qb2_07p4h2kzn0m0000gn/T/test3084029786797422592
dir.getCanonicalPath:/private/var/folders/fq/8q8jpphd2qb2_07p4h2kzn0m0000gn/T/test3084029786797422592

Can see that the Canonical path have /private prefix - Why is that?

I also run ls -al on the absolute path output to see if there is symlink but didn't find any. I'm using mac with scala 2.9.1

Community
  • 1
  • 1
Mzf
  • 5,210
  • 2
  • 24
  • 37

1 Answers1

2

You should not run ls -al on the final path. If you were looking for a symlink you would have to step from the first folder downwards to the file. Luckily this process is quickly over since ls -la / yields amongst others:

lrwxr-xr-x@  1 root  wheel     11 Jan 11 14:16 var -> private/var

Meaning it is in fact a symlink and therefore the canonical paths "expands" the link.

luk2302
  • 55,258
  • 23
  • 97
  • 137