File's path, absolute Path and canonical Path

Keywords: Java

background

In learning about Idea's plug-in development, I used the relevant VirtualFile System, which has a getCanonical Path () method that caught my attention. I found I didn't know.——

Polular Science

First of all, know a few nouns - path, absolute path / relative path, normative path (do not know whether accurate or not)

Then consider the following paths:

  1. c:\temp\file.txt
  2. .\file.txt
  3. c:\temp\MyApp\bin\..\..\file.txt

The first category is path, absolute path and normative path.
The second category belongs to path, relative path.
The third category belongs to path, absolute path.

Based on our own development experience, we find that most cases have been covered, so we can roughly infer that the path contains absolute path / relative path, absolute path contains normative path, and relative path does not contain normative path.

actual combat

/* -------This is a code to regulate the path - -------------------------------------------------------------------------------------------------- */
File file = new File("C:\\Users\\W650\\Desktop\\701Studio\\app.js");
System.out.println("file.getAbsolutePath()  -> " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() -> " + file.getCanonicalPath());
System.out.println("file.getPath()          -> " + file.getPath());

/* -------Output-- */
file.getAbsolutePath()  -> C:\Users\W650\Desktop\701Studio\app.js
file.getCanonicalPath() -> C:\Users\W650\Desktop\701Studio\app.js
file.getPath()          -> C:\Users\W650\Desktop\701Studio\app.js
/* -------This is a code for absolute paths (but not for specifications) - - */
File file = new File("C:\\Users\\W650\\Desktop\\701Studio\\utils\\..\\app.js");
System.out.println("file.getAbsolutePath()  -> " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() -> " + file.getCanonicalPath());
System.out.println("file.getPath()          -> " + file.getPath());

/* -------Output-- */
file.getAbsolutePath()  -> C:\Users\W650\Desktop\701Studio\utils\..\app.js
file.getCanonicalPath() -> C:\Users\W650\Desktop\701Studio\app.js
file.getPath()          -> C:\Users\W650\Desktop\701Studio\utils\..\app.js
/* -------This is a code for relative paths - ---------------------------------------------------------------------------------------------------- */
File file = new File("..\\..\\..\\Test.txt");
System.out.println("file.getAbsolutePath()  -> " + file.getAbsolutePath());
System.out.println("file.getCanonicalPath() -> " + file.getCanonicalPath());
System.out.println("file.getPath()          -> " + file.getPath());

/* -------Output-- */
file.getAbsolutePath()  -> E:\commonWorkspace\IdeaPluginDevGuide\DevGuide-VirtualFileSystem\..\..\..\Test.txt
file.getCanonicalPath() -> E:\Test.txt
file.getPath()          -> ..\..\..\Test.txt

conclusion

  1. The path contains absolute path and relative path, and the absolute path contains normative path.
  2. getPath() returns to the path where the user created the File. getAbsolutePath constructs the absolute path based on the path of the class calling the method + the file separator + the path of creating the File. getCanonical Path () must return to the normal path.

Reference resources

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

Posted by phouchin on Sun, 21 Apr 2019 18:36:33 -0700