为什么File.pathSeparatorChar在Windows上是分号? [英] Why is File.pathSeparatorChar a semicolon on Windows?

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

问题描述

javadoc声明 File.pathSeparatorChar 是:

The javadoc states that File.pathSeparatorChar is:


系统相关的路径分隔符。初始化此字段以包含系统属性path.separator的值的第一个字符。此字符用于分隔作为路径列表给出的文件序列中的文件名。在UNIX系统上,此字符为;在Microsoft Windows系统上,它是;

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

但这似乎很奇怪,因为分号不是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上是分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆