java中uri与url的区别,Java中的资源,URI,URL,路径和文件有什么区别?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值