API語法:
File(String pathname)
通過將給定路徑名字符串轉換為抽象路徑名來創建一個新 File
實例。
public static final String separator
與系統有關的默認名稱分隔符,為了方便,它被表示為一個字符串。此字符串只包含一個字符,即 separatorChar
。
public static final char separatorChar
與系統有關的默認名稱分隔符。此字段被初始化為包含系統屬性 file.separator
值的第一個字符。在 UNIX 系統上,此字段的值為 '/'
;在 Microsoft Windows 系統上,它為 '\\'
。
注意:
路徑名字符串與抽象路徑名之間的轉換與系統有關。將抽象路徑名轉換為路徑名字符串時,每個名稱與下一個名稱之間用一個默認分隔符 隔開。默認名稱分隔符由系統屬性
file.separator
定義,可通過此類的公共靜態字段 separator
和 separatorChar
使其可用。將路徑名字符串轉換為抽象路徑名時,可以使用默認名稱分隔符或者底層系統支持的任何其他名稱分隔符來分隔其中的名稱。
例如,我希望的文件絕對路徑是E:\dev\workspace\iclass_web/conf/filterconfig.xml(計作myPath),有兩種創建File的形式:
1)new File(myPath)不會報錯;
2)new File("E:\dev\workspace\iclass_web/conf/filterconfig.xm")報錯,應修改為new File("E:\\dev\\workspace\\iclass_web/conf/filterconfig.xml"
我的系統是windows32位,io.File的一個字段FileSystem是一個抽象類,FileSystem被一個Win32FileSystem類繼承,從而實現里面的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 }