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

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

问题描述

我正在查看一段Java代码,它将路径作为String并使用获取其URL资源= ClassLoader.getSystemClassLoader()。getResource(pathAsString); ,然后调用 String path = resource.getPath(),最后执行新文件(路径);



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



我现在完全糊涂了 - 主要是因为术语,我猜。有人可以告诉我这些差异,还是提供一些防伪材料的链接?特别是URL到URL和资源到文件?对我来说,分别感觉它们应该是一样的......



getFile() getPath() 在此解释: url.getFile()和getpath()之间的区别是什么?(有趣的是它们似乎都返回了Strings,这可能会增加我的心态...)



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



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



URI 之间的区别Java中的 URL ,前者不编码空格?参看与Java冲突的文件,URI和URL (此答案解释了一般,概念之间的区别这两个术语相当不错: URI标识和URL找到;



最后 - 最重要的是 - 为什么我需要文件对象;为什么资源( URL )不够?(还有资源对象吗?)



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

解决方案

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






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




  • 文件表示可通过文件系统访问的文件或目录

  • 资源是<一个数据对象的href =http://en.wikipedia.org/wiki/Resource_%28Java%29 =noreferrer>通用术语可以由应用程序加载


    • 通常 resources 是随应用程序/库一起分发的文件,并通过类加载机制加载(当它们驻留在类路径上时)


  • URL#getPath 是URL的路径部分的getter(协议://主机/路径查询

  • URL#getFile 根据JavaDoc返回路径+查询



在Java中, URI 只是一个数据结构操纵通用标识符本身。



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



URL可以导致文件系统资源,您可以使用 file:// 协议(因此文件< - > URL 关系)。



还要注意 URL#getFile java.io.File无关







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


这就足够了。只有当您想将资源传递给只能使用文件的某个组件时,才需要从中获取文件。但是,并非所有资源URL都可以转换为文件 s。


并且是否存在资源对象?


从JRE的角度来看,它只是一个术语。一些框架为您提供了这样的类(例如 Spring的资源)。


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);.

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

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...

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...)

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() would give you jar:file:/C:/path/to/my.jar!/com/example/, after all (note the exclamation mark).

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;)

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 Check JvR's answer as it contains more exhaustive and exact explanation!


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

  • 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)
  • URL#getPath is getter on the path part of URL (protocol://host/path?query)
  • URL#getFile as per JavaDoc returns path+query

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

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

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).

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


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

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.

And is there a Resource object?

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天全站免登陆