Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()
2016-10-08 20:19
351 查看
lass.getResourcecan
take a "relative" resource name, which is treated relative to the class's package. Alternatively you can specify an "absolute" resource name by using a leading slash. Classloader resource paths are always deemed to be absolute.
So they are basically equivalent:
foo.bar.Baz.class.getResource("xyz.txt"); foo.bar.Baz.class.getClassLoader().getResource("foo/bar/xyz.txt");
And so are these (but they're different to the above :)
foo.bar.Baz.class.getResource("/data/xyz.txt"); foo.bar.Baz.class.getClassLoader().getResource("data/xyz.txt");
---------------------------------------------------------
getResourceAsStream in static method
Non Static Method
URL url = getClass().getResource("ListStopWords.txt"); File file = new File(url.getPath());
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config.properties");
2. Static Method
InputStream inputStream = FileHelper.class.getClassLoader().getResourceAsStream("config.properties");
-------------------------------------------------------
For Class.getResourceAsStream(String name), if the name parameter doesn't start with a "/", then it's a relative path to the
class's package. If the name parameter starts with a "/", then it's an absolute path.
For ClassLoader.getResourceAsStream(String name), the name parameter is always an absolute path, and it can never start with a "/".
If it does, the resource is never found.
If the file cannot be found, both methods return null and no exception is thrown.
The following program illustrates the difference.
Project structure
ResourceAsStream.java
public class ResourceAsStream { /** * @param args */ public static void main(String[] args) { String path1 = "a.properties"; String path2 = "/a.properties"; String path3 = "test/a.properties"; String path4 = "/test/a.properties"; System.out.println("Class.getResourceAsStream()"); InputStream is = ResourceAsStream.class.getResourceAsStream(path1); System.out.println(path1 + " " + (is != null)); is = ResourceAsStream.class.getResourceAsStream(path2); System.out.println(path2 + " " + (is != null)); is = ResourceAsStream.class.getResourceAsStream(path3); System.out.println(path3 + " " + (is != null)); is = ResourceAsStream.class.getResourceAsStream(path4); System.out.println(path4 + " " + (is != null)); System.out.println(); System.out.println("ClassLoader.getResourceAsStream()"); is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path1); System.out.println(path1 + " " + (is != null)); is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path2); System.out.println(path2 + " " + (is != null)); is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path3); System.out.println(path3 + " " + (is != null)); is = ResourceAsStream.class.getClassLoader().getResourceAsStream(path4); System.out.println(path4 + " " + (is != null)); } }
Result:
Class.getResourceAsStream() a.properties true /a.properties false test/a.properties false /test/a.properties true ClassLoader.getResourceAsStream() a.properties false /a.properties false test/a.properties true/test/a.properties false
相关文章推荐
- getClass().getResourceAsStream VS getClass().getClassLoader().getResourceAsStream
- Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()
- Class.getResourceAsStream vs ClassLoader.getResourceAsStream
- class.getResourceAsStream与class.getClassLoader().getResourceAsStream区别
- android studio下使用getclassloader().getresourceasstream()的问题
- android studio下使用getclassloader().getresourceasstream()的问题
- 性能分析之ClassLoader.getResourceAsStream
- Class.getResourceAsStream() & ClassLoader的getResourceAsStream()
- Java笔记——Class.getResourceAsStream和ClassLoader.getResourceAsStream
- 读取配置文件Properties的一种方案(Thread.currentThread().getContextClassLoader() .getResourceAsStream()
- java 路径、className.class.getResourceAsStream()、ClassLoader.getSystemResourceAsStream() 、FileInputStre
- Class.getResourceAsStream()和Class.getClassLoader().getResourceAsStream()用法
- Class.getResourceAsStream 和 ClassLoader.getResourceAsStream
- className.class.getResourceAsStream()与ClassLoader.getSystemResourceAsStream() 的区别
- ClassLoader.getResourceAsStream(name);路径问题(转)
- JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别
- <转载>SpringMVC(7): 对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解释
- this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream
- ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别
- this.getClass().getResourceAsStream("") 和this.getClass().getClassLoader()getResourceAsStream("")的区别