Why does File.pathSeparatorChar have a semicolon in Windows?

The javadoc states that File.pathSeparatorChar :

System-dependent path separator character. This field is initialized to contain the first character of the value of the system.separator property. This symbol is used to separate file names in a sequence of files specified as a list of paths. On UNIX systems, this symbol:; on Microsoft Windows systems this ; .

But this seems strange, because the semicolon is not a forbidden character for Windows paths (for links, this is \ / : * ? " < > | , Cf is the Windows Explorer rename function).

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 was not expected.

So the question is: why is this character not a colon, as on Unix? I could force my code to use a colon, but that seemed to exceed the goal of having a constant in the JDK.

Edit: Reimeus explained why it cannot be a colon on Windows (it's a disk separator), but what I'm really interested in is the reason that it is not a character that cannot appear in the path, such as | .

+5
source share
3 answers

The PATH delimiter has been a semicolon for a very long time, presumably from the very first release of MS-DOS. (I believe, according to Thorsten, Java is simply deferred to the Windows convention, apparently because Java programmers may suggest that they can use pathSeparatorChar to parse the PATH value, and not just to parse the lists of files created by Sam Java.)

The most obvious parameters for such a separator (similar to English) are period, comma and semicolon. The period conflicts with the 8.3 file name format. The choice of a semicolon above the semicolon may well be arbitrary.

In any case , semicolons were not legal characters in file names at that time , so there was no reason to prefer a comma. And, of course, since nowadays both commas and semicolons are legal, we would not get better if they were. :-)

+2
source

You confuse the path separator with the directory separator.

A path separator is what separates the path entries in the PATH environment variable. This is ; on Windows. For instance:

 PATH=C:\Windows;C:\Program Files 

The directory delimiter separates the names of individual folders by specifying a file or folder name. This is \ on Windows. For instance:

 C:\Windows\Temp\Test.txt 

After our discussion in the comments, I finally understood your problem: -D To the question "why", maybe there can only be a Microsoft answer. This - I agree with you - is not a smart idea to use a separator, which is allowed in folder names. Maybe this comes from old days with 8-character names?

The real question should be how you can determine if ; part of a folder or acts as a delimiter. I myself will ask this question because I find it quite interesting.

+8
source

Colon : used to indicate a drive letter

+2
source

All Articles