The javadoc states that File.pathSeparatorChar
is:
The system-dependent path-separator character. This field is initialized to contain the first character of the value of the system property path.separator. This character is used to separate filenames in a sequence of files given as a path list. On UNIX systems, this character is :
; on Microsoft Windows systems it is ;
.
But that seems strange, because a semicolon is not a forbidden character for Windows paths (for references, those are
/
:
*
?
"
<
>
|
, cf the rename feature of Windows Explorer).
For example, with the following code:
String test = "C:\my;valid;path" + File.pathSeparator + "C:\Windows";
String[] tests = test.split(File.pathSeparator);
tests
will contain C:\my
valid
path
C:\Windows
, which isn't what it'd expect.
So the question is: why isn't this character a colon, like on Unix? I could force my code to use a colon, but that seems to defeat the purpose of having a constant in the JDK.
Edit: Reimeus explained why it can't be a colon on Windows (it's the drive separator), but what I'm really interested in is the reason why it's not a character that can't appear in a path, such as |
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…