Java 奇怪的文件协议(protocol)处理是怎么回事?
我的意思是在 Windows 上 UNC 路径变成了 5 个斜线,我明白为什么会这样,但是在 Linux 上绝对路径变成了 file:/local/path/to/file
那不应该有三个斜杠吗?
我假设 Java 的作者并非无能,那么是否可以解释为什么这是可以接受的?
最佳答案
让我们从 documentation of the URI class 开始:
A hierarchical URI is subject to further parsing according to the syntax
[scheme
:
][//
authority][path][?
query][#
fragment]
如您所见,权限是可选的。这由 the URI specification, section 3 支持:
The scheme and path components are required, though the path may be empty (no characters). When authority is present, the path must either be empty or begin with a slash ("/") character. When authority is not present, the path cannot begin with two slash characters ("//").
file:
URI 可以有一个权限分隔符,//
,后面有一个有效的空权限,但它没有用, 所以省略它没有坏处。它仍然是一个完全兼容的 URI。
关于java - 为什么 linux 上的 aFile.toURI().toString() 有一个斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44724990/