API syntax:
File(String pathname)
Creates a newFile
Instance. Public static final StringSeparatorDefault name separator related to the system. For convenience, it is represented as a string. This string contains only one character, that isseparatorChar
. Public static final charSeparatorCharDefault name separator related to the system. This field is initialized to include system attributes.file.separator
The first character of the value. On UNIX systems, the value of this field is'/'
In Microsoft Windows, it is'\\'
.Note:
The conversion between the path name string and the abstract path name is related to the system. When you convert an abstract path name to a path name string, use a defaultDelimiter. Default name Separator by system attributefile.separator
Definition. You can use this public static fieldseparator
AndseparatorChar
Make it available. When converting a path name string to an abstract path name, you can use the default name separator or any other name separator supported by the underlying system to separate the names.
For example, the absolute path of the File I want is E: \ dev \ workspace \ iclass_web/conf/filterconfig. xml (expressed as myPath). There are two File creation methods:
1) new File (myPath) will not report an error;
2) new File ("E: \ dev \ workspace \ iclass_web/conf/filterconfig. xm ") error. You must modify it to new File (" E: \ dev \ workspace \ iclass_web/conf/filterconfig. xml"
My system is Windows 32-bit, And the FileSystem field of io. File is an abstract class. FileSystem is inherited by a Win32FileSystem class to implement the public abstract String normalize (String path); Method in it.
The source code of Win32FileSystem is as follows:
1 private final char slash;
2 private final char altSlash;
3 private final char semicolon;
4 public Win32FileSystem (){
5 slash = (String) AccessController. doPrivileged (new GetPropertyAction (
6 "file. separator"). charAt (0 );
7 semicolon = (String) AccessController
8. doPrivileged (new GetPropertyAction ("path. separator ")))
9. 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 ))
23 return slash + p;
24 else
25 return p;
26}
27
28 /*
29 * Check that the given pathname is normal. If not, invoke the real
30 * normalizer on the part of the pathname that requires normalization. This
31 * way we iterate through the whole pathname string only once.
32 */
33 public String normalize (String path ){
34 int n = path. length ();
35 char slash = this. slash;
36 char altSlash = this. altSlash;
37 char prev = 0;
38 for (int I = 0; I <n; I ++ ){
39 char c = path. charAt (I );
40 if (c = altSlash)
41 return normalize (path, n, (prev = slash )? I-1: I );
42 if (c = slash) & (prev = slash) & (I> 1 ))
43 return normalize (path, n, I-1 );
44 if (c = ':') & (I> 1 ))
45 return normalize (path, n, 0 );
46 prev = c;
47}
48 if (prev = slash)
49 return normalize (path, n, n-1 );
50 return path;
51}