posts - 35, comments - 0, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          File.separator

          Posted on 2012-07-10 17:12 timelyxyz 閱讀(153) 評論(0)  編輯  收藏

          API語法:

          File(String pathname)
          通過將給定路徑名字符串轉(zhuǎn)換為抽象路徑名來創(chuàng)建一個新 File 實(shí)例。
          public static final String separator
          與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符,為了方便,它被表示為一個字符串。此字符串只包含一個字符,即 separatorChar
          public static final char separatorChar
          與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符。此字段被初始化為包含系統(tǒng)屬性 file.separator 值的第一個字符。在 UNIX 系統(tǒng)上,此字段的值為 '/';在 Microsoft Windows 系統(tǒng)上,它為 '\\'。

          注意:

          路徑名字符串與抽象路徑名之間的轉(zhuǎn)換與系統(tǒng)有關(guān)。將抽象路徑名轉(zhuǎn)換為路徑名字符串時,每個名稱與下一個名稱之間用一個默認(rèn)分隔符 隔開。默認(rèn)名稱分隔符由系統(tǒng)屬性 file.separator 定義,可通過此類的公共靜態(tài)字段 separatorseparatorChar 使其可用。將路徑名字符串轉(zhuǎn)換為抽象路徑名時,可以使用默認(rèn)名稱分隔符或者底層系統(tǒng)支持的任何其他名稱分隔符來分隔其中的名稱。

           

          例如,我希望的文件絕對路徑是E:\dev\workspace\iclass_web/conf/filterconfig.xml(計(jì)作myPath),有兩種創(chuàng)建File的形式:

          1)new File(myPath)不會報(bào)錯;

          2)new File("E:\dev\workspace\iclass_web/conf/filterconfig.xm")報(bào)錯,應(yīng)修改為new File("E:\\dev\\workspace\\iclass_web/conf/filterconfig.xml"

           我的系統(tǒng)是windows32位,io.File的一個字段FileSystem是一個抽象類,F(xiàn)ileSystem被一個Win32FileSystem類繼承,從而實(shí)現(xiàn)里面的public abstract String normalize(String path);方法。

           Win32FileSystem部分源碼如下:

           1 private final char slash;  2     private final char altSlash;  3     private final char semicolon;  4
           5     public Win32FileSystem() {  6     slash = ((String) AccessController.doPrivileged(  7               new GetPropertyAction("file.separator"))).charAt(0);  8     semicolon = ((String) AccessController.doPrivileged(  9               new GetPropertyAction("path.separator"))).charAt(0); 10     altSlash = (this.slash == '\\') ? '/' : '\\'; 11     } 12 13     private boolean isSlash(char c) { 14     return (c == '\\') || (c == '/'); 15     } 16 17     private boolean isLetter(char c) { 18     return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')); 19     } 20 21     private String slashify(String p) { 22     if ((p.length() > 0) && (p.charAt(0) != slash)) return slash + p; 23     else return p; 24     } 25    26     /* Check that the given pathname is normal.  If not, invoke the real 27        normalizer on the part of the pathname that requires normalization. 28        This way we iterate through the whole pathname string only once. */ 29     public String normalize(String path) { 30     int n = path.length(); 31     char slash = this.slash; 32     char altSlash = this.altSlash; 33     char prev = 0; 34     for (int i = 0; i < n; i++) { 35         char c = path.charAt(i); 36         if (c == altSlash) 37         return normalize(path, n, (prev == slash) ? i - 1 : i); 38         if ((c == slash) && (prev == slash) && (i > 1)) 39         return normalize(path, n, i - 1); 40         if ((c == ':') && (i > 1)) 41         return normalize(path, n, 0); 42         prev = c; 43     } 44     if (prev == slash) return normalize(path, n, n - 1); 45     return path; 46     }

           


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 体育| 沂水县| 龙州县| 高安市| 鹤庆县| 安远县| 彰化市| 永胜县| 正蓝旗| 孝昌县| 阿拉善左旗| 张家川| 玛多县| 察隅县| 福海县| 乌审旗| 呼图壁县| 三都| 旬阳县| 岳池县| 郧西县| 雅安市| 平和县| 城固县| 辰溪县| 阜南县| 永清县| 石狮市| 墨江| 抚松县| 咸丰县| 海兴县| 桐庐县| 始兴县| 灵宝市| 娱乐| 双鸭山市| 镇远县| 湘潭县| 比如县| 铜川市|