Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

java - Why is File.pathSeparatorChar a semicolon on Windows?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're confusing the path separator with the directory separator.

The path separator is what separates paths entries in the PATH environment variable. It is ; on Windows. For example:

PATH=C:Windows;C:Program Files

The directory separator separates single folder names when specifying a file or folder name. It is on Windows. For example:

C:WindowsTempTest.txt

After our discussion in the comments I finally understood your problem :-D The question "why" can probably only be answered by Microsoft. It is - I agree with you - not a smart idea to use a separator that's allowed in folder names. Maybe this comes from the old days of 8-character names?

The real question should be how you can determine whether the ; is part of the folder or acts as the separator. I'm going to ask that question myself, because I find this rather interesting.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...