简体   繁体   English

javafx-文件的URI和absolutepath之间的区别

[英]javafx- difference between URI and absolutepath for a file

I wanted to use an absolute path for Image URL in javafx . 我想为javafx中的Image URL使用绝对路径。 this gives me a way to do that . 给了我一个办法。

File file = new File(System.getProperty ("user.dir")+ "\\profile" + "\\chat3.png");
    System.out.println(file);
    System.out.println(file.toURI().toURL().toExternalForm());

These codes generate the following output: 这些代码生成以下输出:

C:\\Users\\myUsername\\Downloads\\Compressed\\15Dec\\profile\\chat3.png file:/C:/Users/myUsername/Downloads/Compressed/15Dec/profile/chat3.png C:\\ Users \\ myUsername \\ Downloads \\ Compressed \\ 15Dec \\ profile \\ chat3.png文件:/ C:/Users/myUsername/Downloads/Compressed/15Dec/profile/chat3.png

what is the difference between these two lines? 这两行有什么区别?

The file.toURI() handles special character conversions, so in general calling file.toURI().toURL() is a better option since it will convert for example white spaces to %20 , For example if file name is "chat version 3.png" converts to : file.toURI()处理特殊字符转换,因此通常调用file.toURI()。toURL()是一个更好的选择,因为它将例如将空格转换为%20,例如,如果文件名是“聊天版本3” .png”转换为:

file:/C:/Users/myUsername/Downloads/Compressed/15Dec/profile/chat%20version3.png 文件:/ C:/Users/myUsername/Downloads/Compressed/15Dec/profile/chat%20version3.png

Both lines point to the same resource file, they just use different syntaxes to do so. 这两行都指向同一个资源文件,只是使用不同的语法。

C:\\Users\\myUsername\\Downloads\\Compressed\\15Dec\\profile\\chat3.png C:\\ Users \\ myUsername \\ Downloads \\ Compressed \\ 15Dec \\ profile \\ chat3.png

Is an MS-DOS style file path . MS-DOS样式的文件路径

It is a syntax used by Windows for identifying a file location. 它是Windows用于标识文件位置的语法。 Other operating systems will use a different format (for instance Unix uses forward slashes rather than back slashes). 其他操作系统将使用不同的格式(例如Unix使用正斜杠而不是反斜杠)。

file:/C:/Users/myUsername/Downloads/Compressed/15Dec/profile/chat3.png 文件:/ C:/Users/myUsername/Downloads/Compressed/15Dec/profile/chat3.png

Is a Uniform Resource Identifier (URI) . 统一资源标识符(URI) It conforms to a standard syntax for identifying a resource. 它符合用于标识资源的标准语法 The URI is a standard format and not specific to a given operating system (it will always use forward slashes). URI是一种标准格式,并不特定于给定的操作系统(它将始终使用正斜杠)。

In this case the URI type is a Uniform Resource Locator (URL) that identifies a resource accessed via the file protocol at the provided path. 在这种情况下,URI类型是统一资源定位符(URL),用于标识在提供的路径上通过文件协议访问的资源。 The syntax for a URI is: URI的语法为:

scheme:[//[user:password@]host[:port]][/]path[?query][#fragment] 方案:[// [用户:密码@]主机[:端口]] [/]路径[?查询] [#fragment]

To understand more, read the linked articles. 要了解更多信息,请阅读链接的文章。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM