Java 中的资源、URI、URL、路径和文件有什么区别? [英] What's the difference between a Resource, URI, URL, Path and File in Java?

查看:32
本文介绍了Java 中的资源、URI、URL、路径和文件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在查看一段 Java 代码,它将路径作为字符串并使用 URL resource = ClassLoader.getSystemClassLoader().getResource(pathAsString);,然后调用String path = resource.getPath(),最后执行new File(path);.

I'm looking at a piece of Java code right now, and it takes a path as a String and gets its URL using URL resource = ClassLoader.getSystemClassLoader().getResource(pathAsString);, then calls String path = resource.getPath() and finally executes new File(path);.

哦,还有对URL url = resource.toURI();String file = resource.getFile() 的调用.

Oh, and there are also calls to URL url = resource.toURI(); and String file = resource.getFile().

我现在完全糊涂了——我猜主要是因为术语.有人可以引导我了解这些差异,或提供一些指向防伪材料的链接吗?尤其是 URI 到 URL 和资源到文件?对我来说,感觉它们应该是一样的东西,分别...

I'm totally confused right now - mostly because of the terminology, I guess. Can someone please walk me through the differences, or provide a few links to Dummy-proof material? Especially URI to URL and Resource to File? To me, it feels like they should be the same thing, respectively...

getFile()getPath() 之间的区别解释如下:url.getFile() 和 getpath() 之间有什么区别?(有趣的是,它们似乎都返回字符串,这可能给我增加了很多心态……)

The difference between getFile() and getPath() is explained here: What's the difference between url.getFile() and getpath()? (Interestingly they both seem to return Strings, which probably adds a whole lot to my state of mind...)

现在,如果我有一个引用 jar 文件中的类或包的定位器,这两个(即文件字符串的路径)会不同吗?

Now, if I have a locator that references a class or package in a jar file, will those two (i.e. path an file strings) differ?

resource.toString() 会给你 jar:file:/C:/path/to/my.jar!/com/example/,毕竟(注意感叹号).

resource.toString() would give you jar:file:/C:/path/to/my.jar!/com/example/, after all (note the exclamation mark).

URIURL Java 之间的区别是前者不编码空格吗?参见Java 中的文件、URI 和 URL 冲突(此答案解释了一般性、概念性这两个术语相当不错:URI 标识和 URL定位;)

Is the difference between URI and URL in Java that the former doesn't encode spaces? Cf. Files, URIs, and URLs conflicting in Java (This answer explains the general, conceptual difference between the two terms fairly well: URIs identify and URLs locate;)

最后 - 也是最重要的 - 为什么我需要 File 对象;为什么 Resource (URL) 不够用?(还有 Resource 对象吗?)

Lastly - and most importantly - why do I need File object; why isn't a Resource (URL) enough? (And is there a Resource object?)

对不起,如果这个问题有点无组织;它只是反映了我的困惑... :)

Sorry if this question is a bit unorganized; it just reflects the confusion I have... :)

推荐答案

UPDATE 2017-04-12 检查 JvR 的回答,因为它包含更详尽和准确的解释!

UPDATE 2017-04-12 Check JvR's answer as it contains more exhaustive and exact explanation!

请注意,我不认为自己 100% 有能力回答,但这里有一些评论:

Please note that I do not consider myself 100% competent to answer, but nevertheless here are some comments:

  • File 表示可通过文件系统访问的文件或目录
  • resource 是一个通用术语可由应用程序加载的数据对象
    • 通常资源是与应用程序/库一起分发并通过类加载机制加载的文件(当它们驻留在类路径上时)
    • File represents a file or directory accessible via file system
    • resource is a generic term for a data object which can be loaded by the application
      • usually resources are files distributed with the application / library and loaded via class-loading mechanism (when they reside on class-path)

      在 Java 中,URI 只是用于操作通用标识符本身的数据结构.

      In Java, URI is just a data structure for manipulating the generic identifier itself.

      URL 实际上是一个资源定位器,并为您提供通过注册的 URLStreamHandler 实际读取资源的功能.

      URL on the other hand is really a resource locator and offers you features to actually read the resource via registered URLStreamHandlers.

      URL 可以指向文件系统资源,您可以使用 file:// 协议为每个文件系统资源构建 URL(因此 File <-> <代码>URL 关系).

      URLs can lead to file-system resources and you can construct URL for every file system resource by using file:// protocol (hence File <-> URL relation).

      另请注意,URL#getFilejava.io.File 无关.

      Also be aware that that URL#getFile is unrelated to java.io.File.

      为什么我需要File对象;为什么一个资源(URL)还不够?

      Why do I need File object; why isn't a Resource (URL) enough?

      够了.只有当您想将资源传递给某个只能处理文件的组件时,您才需要从中获取 File.然而,并非所有资源 URL 都可以转换为 Files.

      It is enough. Only if you want to pass the resource to some component which can work only with files, you need to get File from it. However not all resource URLs can be converted to Files.

      还有资源对象吗?

      从 JRE 的角度来看,它只是一个术语.一些框架为你提供了这样的类(例如 Spring's Resource).

      From the JRE point of view, it's just a term. Some frameworks provide you with such class (e.g. Spring's Resource).

      这篇关于Java 中的资源、URI、URL、路径和文件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆