简体   繁体   English

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

[英]Why would aFile.toURI().toString() on linux have a single slash?

What is the deal with Java's bizarre file protocol handling? Java奇怪的文件协议处理有什么用? I mean on windows UNC path's get turned into 5 slashes, and I get why that happens, but on linux an absolute path gets turned into file:/local/path/to/file Shouldn't that have three slashes? 我的意思是在Windows上UNC路径变成了5个斜杠,我明白为什么会发生这种情况,但是在linux上,绝对路径变成了file:/local/path/to/file不应该有三个斜杠吗? I'm assuming the authors of Java aren't incompetent, so is there an explanation for why that's acceptable? 我假设Java的作者并不称职,那么为什么这是可以接受的呢?

Let's start with the documentation of the URI class : 让我们从URI类文档开始:

A hierarchical URI is subject to further parsing according to the syntax 分层URI根据语法进行进一步解析

[ scheme : ][ // authority ][ path ][ ? [ scheme : ] [ // 权限 ] [ 路径 ] [ ? query ][ # fragment ] 查询 ] [ # 片段 ]

As you can see, the authority is optional. 如您所见,权限是可选的。 This is supported by the URI specification, section 3 : 这支持URI规范,第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 ("//"). 当权限不存在时,路径不能以两个斜杠字符(“//”)开头。

A file: URI can have an authority separator, // , with an effectively empty authority after it, but it serves no purpose, so there is no harm in omitting it. 一个file: URI 可以有一个权限分隔符// ,后面有一个有效的空权限,但它没有用处,所以省略它没有任何害处。 It's still a fully compliant URI. 它仍然是完全兼容的URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linux上的Java File.toURI()问题 - Java File.toURI() issue on Linux 为什么 toUri().toURL() 在开始时加载本地路径? - Why toUri().toURL() loads local path on begining? 为什么 toURI() 方法给我错误“方法调用‘toURI’可能产生‘NullPointerException’”? - Why the toURI() method is giving me the erro "Method invocation 'toURI' may produce 'NullPointerException'"? 为什么不透明URI不能以单斜杠('/')开头,而不是以双斜杠('//')开头? URI仅有斜杠的任何实际示例吗? - Why opaque URI shall not begin with single slash ('/'), not double-slash ('//')? Any practical example of URI with just one slash? 为什么我必须在Split方法中使用Array.toString? - Why do I have to use Array.toString at Split method? InetAddress.toString()返回正斜杠 - InetAddress.toString() returns a forward slash 为什么此命令的退出代码为1 - Why would this command have exit code 1 有2个toString方法吗? - Have 2 toString method? 当我尝试比较斜杠时,为什么Java无法识别字符比较? - Why would java won't recognize a character comparision when I try to compare a slash? 为什么path.toString()的结果在Linux上无法显示所有字符但在Windows上可以显示 - Why are results of path.toString() failing to show all characters on Linux but ok on windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM