Java URL 分隔符或 File.separator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34614336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:07:19  来源:igfitidea点击:

Java URL separator or File.separator

java

提问by Ondrej Tokar

I have checked similar qestions where they suggested to use Stringas a parameter. But that is what I do.

我已经检查过类似的问题,他们建议将其String用作参数。但这就是我所做的。

My Code:

我的代码:

 uri.lastIndexOf(File.separator)

Where uriis: file:/C:/Users/[email protected]/workspace/Maersk_Line_GaTracking/bin/

在哪里urifile:/C:/Users/[email protected]/workspace/Maersk_Line_GaTracking/bin/

Maybe that is because Windows uses back slashes as file separator. But why then, when I retrieve the URI, I am getting forward slashes?

也许这是因为 Windows 使用反斜杠作为文件分隔符。但是为什么那么,当我检索 URI 时,我得到了正斜杠?

String uri = DataRetrieval.class.getProtectionDomain().getCodeSource().getLocation() + "";

回答by Arnaud

CodeSource.getLocation() 

returns an URLobject.

返回一个 URL对象。

Separators in URLs are forward slashes.

URL 中的分隔符是正斜杠。

回答by Matt Timmermans

There's a difference between file names and URIs. File names use \as a separator on Windows, but URIs alwaysuse /.

文件名和 URI 之间存在差异。文件名\在 Windows 上用作分隔符,但 URI始终使用/.

So if you're splitting file names, search for File.Separator, and if you're splitting URIs, search for '/'

因此,如果要拆分文件名,请搜索 File.Separator,如果要拆分 URI,请搜索 '/'

OH, I should mention: /alsoworks as a separator in most windows APIs, so for filenames, it's usually a good idea to map all the forward slashes to File.Separator before you start messing with it.

哦,我应该提到: 在大多数 Windows API 中/可用作分隔符,因此对于文件名,通常最好在开始使用 File.Separator 之前将所有正斜杠映射到 File.Separator。

回答by whiskeyspider

URIs are defined to use /as their component separator.

URI 被定义为/用作它们的组件分隔符。