java - 为什么 linux 上的 aFile.toURI().toString() 有一个斜杠?

标签 java linux uri file-uri

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/

相关文章:

linux - 房地产linux备份解决方案

wpf - ImageSource 上的转换器 - 将图像转换为灰度

android - 从 sdcard marshmallow 文件的 URI 获取真实路径

java - jar执行期间资源路径改变

java - 如何根据If Else函数刷新GridPane?

java - 如何将 java JAXB Schema 类导出到 xsd 文件?

java - 如何在密码查询中在字段级别分页?

java - Linux:如何获取所有可见窗口的列表

linux - 为什么memory NX在Linux内核中需要硬件支持?

android - 在 logcat 中抑制/混淆 URL