getResourceAsStream()地狱 [英] getResourceAsStream() hell

查看:128
本文介绍了getResourceAsStream()地狱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下.getResourceAsStream()是如何工作的。我试图弄清楚痛苦的事实,在某些情况下getClass()。getResourceAsStream(name);在eclipse中可以正常工作,但在部署版本(NPE)中则不行。我面临的另一个事实是,有时候getClass()。getResource(name);返回一个完全有效的url但getClass()。getResourceAsStream(name);返回null。有人写了一个库,我只是传递包(路径)和文件名并接收流而没有唠叨吗?

Can someone explain me please how .getResourceAsStream() is really working. I try to figure out the painful facts, that in some cases getClass().getResourceAsStream(name); will work fine in eclipse but not in the deployed version (NPE). Another fact I am facing is, that sometimes getClass().getResource(name); returns a perfectly valid url but getClass().getResourceAsStream(name); returns null. Has someone written a library where I just pass the package (path) and filename and receive the stream without nagging?

编辑:这是一般的,每次我都会影响我使用getResourceAsStream()。是的,我可以提供一个针对实际情况的代码,但是直到我不理解getResourceAsStream是如何工作的,这对我下次没有帮助。

This is something general which affects me everytime I have to use getResourceAsStream(). Yes I could provide a code snipped for the actual situation but this is not helping me for the next time until I do not understand how getResourceAsStream is working.

编辑2:和有什么区别:

EDIT 2: And what is the difference between:

MyClass.class.getResourceAsStream(...)
this.getClass().getResourceAsStream(...),    
MyClass.class.getClassLoader().getResourceAsStream(...)
Thread.currentThread().getContextClassLoader().getResourceAsStream(...)

编辑3:
此外,当 some.package.SomeClass.class.getResourceAsStream( /path/only/holding/resource.files)无法在没有类但只包含文件的类路径中加载资源。这个变种能够这样做: ClassLoader.class.getResourceAsStream(/ path / only / holding / resource.files)这让我很困惑,所以我真的很喜欢像spring这样的类似于schema的类路径:

EDIT 3: Also, while some.package.SomeClass.class.getResourceAsStream("/path/only/holding/resource.files") is not able to load resources in a classpath holding no classes but only files. This variant is able to do so: ClassLoader.class.getResourceAsStream("/path/only/holding/resource.files") this confuses me a lot so I would really prefere something like spring does with URI the schema classpath:

推荐答案

规则非常简单。假设您的班级在 com.foo 包中。

The rule is quite simple. Let's say your class is in the package com.foo.

调用 getResource( bar / bla.txt)(没有前导 / )此类将在 bla.txt中查找文件包中的 com.foo.bar 。因此路径相对于类的包。类路径用于查找文件,就像类路径将用于查找类一样。

Calling getResource("bar/bla.txt") (no leading /) on this class will look for a file in bla.txt in the package com.foo.bar. The path is thus relative to the package of the class. The classpath is used to find the file, just as the classpath would be used to find a class.

调用 getResource(/ bar / bla此类上的.txt)(领先 / )将在 bla.txt 栏中的$ c> 。因此,路径是一个绝对路径,从类路径的根开始。类路径用于查找文件,就像类路径将用于查找类一样。

Calling getResource("/bar/bla.txt") (leading /) on this class will look for a file in bla.txt in the package bar. The path is thus an absolute path, starting at the root of the classpath. The classpath is used to find the file, just as the classpath would be used to find a class.

并且您可能没有包含的路径。 .. 就像使用文件系统路径一样。

And you may not have paths containing . or .. like you would have with filesystem paths.

这篇关于getResourceAsStream()地狱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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