Why is File.pathSeparatorChar a semicolon on Windows?

后端 未结 3 1891
醉梦人生
醉梦人生 2020-12-21 00:42

The javadoc states that File.pathSeparatorChar is:

The system-dependent path-separator character. This field is initialized to contain the first chara

相关标签:
3条回答
  • 2020-12-21 01:14

    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:\Windows\Temp\Test.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.

    0 讨论(0)
  • 2020-12-21 01:15

    A colon : is used to denote a drive letter

    0 讨论(0)
  • 2020-12-21 01:17

    The PATH separator has been a semicolon for a very long time, presumably since the very first release of MS-DOS. (I'm assuming, as per Thorsten's answer, that Java simply defered to the Windows convention, presumably because Java programmers are likely to assume that they can use pathSeparatorChar to parse the value of PATH rather than only to parse file lists produced by Java itself.)

    The most obvious options for such a separator (by analogy with English) are the period, the comma, and the semicolon. The period would conflict with the 8.3 file name format. The choice of the semicolon over the comma may well have been arbitrary.

    At any rate, semicolons were not legal characters in file names at that time, so there was no reason to prefer the comma. And, of course, since nowadays both commas and semicolons are legal, we wouldn't be any better off if they had. :-)

    0 讨论(0)
提交回复
热议问题