本文介绍了为什么File.pathSeparatorChar在Windows上是分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javadoc声明是:

The javadoc states that File.pathSeparatorChar is:

但这似乎很奇怪,因为分号不是Windows路径的禁用字符(对于引用,它们是 \ / * < > | ,参见重命名功能Windows资源管理器)。

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).

例如,使用以下代码:

String test = "C:\\my;valid;path" + File.pathSeparator + "C:\\Windows";
String[] tests = test.split(File.pathSeparator);

tests 将包含 C:\\ my 有效 路径 C:\\ Windows ,这不是它所期望的。

tests will contain C:\\my valid path C:\\Windows, which isn't what it'd expect.

所以问题是:为什么这个字符不是冒号,就像在Unix上一样?我可以强制我的代码使用冒号,但是似乎打败了在JDK中保持常量的目的。

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.

编辑:Reimeus解释了为什么它不能成为Windows上的冒号(它是驱动器分隔符),但我真正感兴趣的是它不是一个不能出现在路径中的角色的原因,例如 |

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 |.

推荐答案

PATH分隔符很长一段时间都是分号,大概是从MS-DOS的第一个版本开始。 (根据Thorsten的回答,我假设Java只是简单地遵循Windows约定,大概是因为Java程序员可能会认为他们可以使用 pathSeparatorChar 来解析值PATH而不是仅解析由Java本身生成的文件列表。)

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.)

这种分隔符的最明显选项(通过类比英文)是句点,逗号和分号。该期间将与8.3文件名格式冲突。分号对逗号的选择可能是任意的。

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. :-)

这篇关于为什么File.pathSeparatorChar在Windows上是分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 10:15