筆記

          way

          區分getPath(), getAbsolutePath(), getCanonicalPath()

          來自

          http://stackoverflow.com/questions/1099300/whats-the-difference-between-getpath-getabsolutepath-and-getcanonicalpath

          C:\temp\file.txt" - this is a path, an absolute path, a canonical path

          .\file.txt This is a path, It's not an absolute path nor canonical path.

          C:\temp\myapp\bin\..\\..\file.txt This is a path, and an absolute path, it's not a canonical path

          Canonical path is always an absolute path.

          Converting from a path to a canonical path makes it absolute (通常會處理改變當前目錄,所以像. ./file.txt 變為c:/temp/file.txt). The canonical path of a file just "purifies" the path, 去除和解析類似“ ..\” and resolving symlinks(on unixes)

          In short:

          • getPath() gets the path string that the File object was constructed with, and it may be relative current directory.
          • getAbsolutePath() gets the path string after resolving it against the current directory if it's relative, resulting in a fully qualified path.
          • getCanonicalPath() gets the path string after resolving any relative path against current directory, and removes any relative pathing (. and ..), and any file system links to return a path which the file system considers the canonical means to reference the file system object to which it points.

          Also, each of this has a File equivalent which returns the corresponding File object.

          The best way I have found to get a feel for things like this is to try them out:

          import java.io.File;
          public class PathTesting {
                  public static void main(String [] args) {
                          File f = new File("test/.././file.txt");
                          System.out.println(f.getPath());
                          System.out.println(f.getAbsolutePath());
                          try {
                                  System.out.println(f.getCanonicalPath());
                          }
                          catch(Exception e) {}
                  }
          }

          Your output will be something like:

          test\..\.\file.txt
          C:\projects\sandbox\trunk\test\..\.\file.txt
          C:\projects\sandbox\trunk\file.txt

          So, getPath() gives you the path based on the File object, which may or may not be relative; getAbsolutePath() gives you an absolute path to the file; and getCanonicalPath() gives you the unique absolute path to the file. Notice that there are a huge number of absolute paths that point to the same file, but only one canonical path.

          When to use each? Depends on what you're trying to accomplish, but if you were trying to see if two Files are pointing at the same file on disk, you could compare their canonical paths.

          posted on 2011-06-24 13:42 yuxh 閱讀(507) 評論(0)  編輯  收藏 所屬分類: jdkwork

          導航

          <2011年6月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          收藏夾

          博客

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 遵义县| 姜堰市| 托克逊县| 如东县| 沐川县| 天峻县| 桓台县| 宝清县| 万载县| 桓仁| 舒兰市| 门源| 唐河县| 林西县| 茂名市| 游戏| 五常市| 霸州市| 郑州市| 虞城县| 岱山县| 岳阳县| 临汾市| 怀安县| 普安县| 泸定县| 邓州市| 孝义市| 雷州市| 丰城市| 江川县| 淅川县| 威宁| 进贤县| 轮台县| 鹿邑县| 涞源县| 平顺县| 沂水县| 米林县| 海淀区|