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)字段 separator
和 separatorChar
使其可用。將路徑名字符串轉(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 }
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 }