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:
- c:\temp\file.txt
- .\file.txt
- 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
- The path contains absolute path and relative path, and the absolute path contains normative path.
- 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?